mvpp2.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189
  1. /*
  2. * Driver for Marvell PPv2 network controller for Armada 375 SoC.
  3. *
  4. * Copyright (C) 2014 Marvell
  5. *
  6. * Marcin Wojtas <mw@semihalf.com>
  7. *
  8. * U-Boot version:
  9. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <net.h>
  20. #include <netdev.h>
  21. #include <config.h>
  22. #include <malloc.h>
  23. #include <asm/io.h>
  24. #include <linux/errno.h>
  25. #include <phy.h>
  26. #include <miiphy.h>
  27. #include <watchdog.h>
  28. #include <asm/arch/cpu.h>
  29. #include <asm/arch/soc.h>
  30. #include <linux/compat.h>
  31. #include <linux/mbus.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* Some linux -> U-Boot compatibility stuff */
  34. #define netdev_err(dev, fmt, args...) \
  35. printf(fmt, ##args)
  36. #define netdev_warn(dev, fmt, args...) \
  37. printf(fmt, ##args)
  38. #define netdev_info(dev, fmt, args...) \
  39. printf(fmt, ##args)
  40. #define netdev_dbg(dev, fmt, args...) \
  41. printf(fmt, ##args)
  42. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  43. #define __verify_pcpu_ptr(ptr) \
  44. do { \
  45. const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
  46. (void)__vpp_verify; \
  47. } while (0)
  48. #define VERIFY_PERCPU_PTR(__p) \
  49. ({ \
  50. __verify_pcpu_ptr(__p); \
  51. (typeof(*(__p)) __kernel __force *)(__p); \
  52. })
  53. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
  54. #define smp_processor_id() 0
  55. #define num_present_cpus() 1
  56. #define for_each_present_cpu(cpu) \
  57. for ((cpu) = 0; (cpu) < 1; (cpu)++)
  58. #define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
  59. #define CONFIG_NR_CPUS 1
  60. #define ETH_HLEN ETHER_HDR_SIZE /* Total octets in header */
  61. /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
  62. #define WRAP (2 + ETH_HLEN + 4 + 32)
  63. #define MTU 1500
  64. #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
  65. #define MVPP2_SMI_TIMEOUT 10000
  66. /* RX Fifo Registers */
  67. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  68. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  69. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  70. #define MVPP2_RX_FIFO_INIT_REG 0x64
  71. /* RX DMA Top Registers */
  72. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  73. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  74. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  75. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  76. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  77. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  78. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  79. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  80. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  81. #define MVPP2_RXQ_POOL_SHORT_MASK 0x700000
  82. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  83. #define MVPP2_RXQ_POOL_LONG_MASK 0x7000000
  84. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  85. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  86. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  87. /* Parser Registers */
  88. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  89. #define MVPP2_PRS_PORT_LU_MAX 0xf
  90. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  91. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  92. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  93. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  94. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  95. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  96. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  97. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  98. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  99. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  100. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  101. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  102. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  103. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  104. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  105. /* Classifier Registers */
  106. #define MVPP2_CLS_MODE_REG 0x1800
  107. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  108. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  109. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  110. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  111. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  112. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  113. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  114. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  115. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  116. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  117. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  118. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  119. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  120. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  121. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  122. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  123. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  124. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  125. /* Descriptor Manager Top Registers */
  126. #define MVPP2_RXQ_NUM_REG 0x2040
  127. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  128. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  129. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  130. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  131. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  132. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  133. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  134. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  135. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  136. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  137. #define MVPP2_RXQ_THRESH_REG 0x204c
  138. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  139. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  140. #define MVPP2_RXQ_INDEX_REG 0x2050
  141. #define MVPP2_TXQ_NUM_REG 0x2080
  142. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  143. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  144. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  145. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  146. #define MVPP2_TXQ_THRESH_REG 0x2094
  147. #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
  148. #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
  149. #define MVPP2_TXQ_INDEX_REG 0x2098
  150. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  151. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  152. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  153. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  154. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  155. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  156. #define MVPP2_TXQ_PENDING_REG 0x20a0
  157. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  158. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  159. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  160. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  161. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  162. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  163. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  164. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  165. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  166. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  167. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  168. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  169. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  170. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  171. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  172. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  173. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  174. /* MBUS bridge registers */
  175. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  176. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  177. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  178. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  179. /* Interrupt Cause and Mask registers */
  180. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  181. #define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
  182. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  183. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  184. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  185. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  186. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  187. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  188. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  189. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  190. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  191. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  192. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  193. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  194. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  195. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  196. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  197. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  198. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  199. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  200. /* Buffer Manager registers */
  201. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  202. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  203. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  204. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  205. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  206. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  207. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  208. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  209. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  210. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  211. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  212. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  213. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  214. #define MVPP2_BM_START_MASK BIT(0)
  215. #define MVPP2_BM_STOP_MASK BIT(1)
  216. #define MVPP2_BM_STATE_MASK BIT(4)
  217. #define MVPP2_BM_LOW_THRESH_OFFS 8
  218. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  219. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  220. MVPP2_BM_LOW_THRESH_OFFS)
  221. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  222. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  223. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  224. MVPP2_BM_HIGH_THRESH_OFFS)
  225. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  226. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  227. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  228. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  229. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  230. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  231. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  232. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  233. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  234. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  235. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  236. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  237. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  238. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  239. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  240. #define MVPP2_BM_MC_RLS_REG 0x64c4
  241. #define MVPP2_BM_MC_ID_MASK 0xfff
  242. #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
  243. /* TX Scheduler registers */
  244. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  245. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  246. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  247. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  248. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  249. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  250. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  251. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  252. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  253. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  254. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  255. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  256. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  257. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  258. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  259. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  260. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  261. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  262. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  263. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  264. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  265. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  266. /* TX general registers */
  267. #define MVPP2_TX_SNOOP_REG 0x8800
  268. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  269. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  270. /* LMS registers */
  271. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  272. #define MVPP2_SRC_ADDR_HIGH 0x28
  273. #define MVPP2_PHY_AN_CFG0_REG 0x34
  274. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  275. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  276. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  277. /* Per-port registers */
  278. #define MVPP2_GMAC_CTRL_0_REG 0x0
  279. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  280. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  281. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  282. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  283. #define MVPP2_GMAC_CTRL_1_REG 0x4
  284. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  285. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  286. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  287. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  288. #define MVPP2_GMAC_SA_LOW_OFFS 7
  289. #define MVPP2_GMAC_CTRL_2_REG 0x8
  290. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  291. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  292. #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
  293. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  294. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  295. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  296. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  297. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  298. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  299. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  300. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  301. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  302. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  303. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  304. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  305. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  306. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  307. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  308. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  309. /* Descriptor ring Macros */
  310. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  311. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  312. /* SMI: 0xc0054 -> offset 0x54 to lms_base */
  313. #define MVPP2_SMI 0x0054
  314. #define MVPP2_PHY_REG_MASK 0x1f
  315. /* SMI register fields */
  316. #define MVPP2_SMI_DATA_OFFS 0 /* Data */
  317. #define MVPP2_SMI_DATA_MASK (0xffff << MVPP2_SMI_DATA_OFFS)
  318. #define MVPP2_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
  319. #define MVPP2_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
  320. #define MVPP2_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
  321. #define MVPP2_SMI_OPCODE_READ (1 << MVPP2_SMI_OPCODE_OFFS)
  322. #define MVPP2_SMI_READ_VALID (1 << 27) /* Read Valid */
  323. #define MVPP2_SMI_BUSY (1 << 28) /* Busy */
  324. #define MVPP2_PHY_ADDR_MASK 0x1f
  325. #define MVPP2_PHY_REG_MASK 0x1f
  326. /* Various constants */
  327. /* Coalescing */
  328. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  329. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  330. #define MVPP2_RX_COAL_PKTS 32
  331. #define MVPP2_RX_COAL_USEC 100
  332. /* The two bytes Marvell header. Either contains a special value used
  333. * by Marvell switches when a specific hardware mode is enabled (not
  334. * supported by this driver) or is filled automatically by zeroes on
  335. * the RX side. Those two bytes being at the front of the Ethernet
  336. * header, they allow to have the IP header aligned on a 4 bytes
  337. * boundary automatically: the hardware skips those two bytes on its
  338. * own.
  339. */
  340. #define MVPP2_MH_SIZE 2
  341. #define MVPP2_ETH_TYPE_LEN 2
  342. #define MVPP2_PPPOE_HDR_SIZE 8
  343. #define MVPP2_VLAN_TAG_LEN 4
  344. /* Lbtd 802.3 type */
  345. #define MVPP2_IP_LBDT_TYPE 0xfffa
  346. #define MVPP2_CPU_D_CACHE_LINE_SIZE 32
  347. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  348. /* Timeout constants */
  349. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  350. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  351. #define MVPP2_TX_MTU_MAX 0x7ffff
  352. /* Maximum number of T-CONTs of PON port */
  353. #define MVPP2_MAX_TCONT 16
  354. /* Maximum number of supported ports */
  355. #define MVPP2_MAX_PORTS 4
  356. /* Maximum number of TXQs used by single port */
  357. #define MVPP2_MAX_TXQ 8
  358. /* Maximum number of RXQs used by single port */
  359. #define MVPP2_MAX_RXQ 8
  360. /* Default number of TXQs in use */
  361. #define MVPP2_DEFAULT_TXQ 1
  362. /* Dfault number of RXQs in use */
  363. #define MVPP2_DEFAULT_RXQ 1
  364. #define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
  365. /* Total number of RXQs available to all ports */
  366. #define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
  367. /* Max number of Rx descriptors */
  368. #define MVPP2_MAX_RXD 16
  369. /* Max number of Tx descriptors */
  370. #define MVPP2_MAX_TXD 16
  371. /* Amount of Tx descriptors that can be reserved at once by CPU */
  372. #define MVPP2_CPU_DESC_CHUNK 64
  373. /* Max number of Tx descriptors in each aggregated queue */
  374. #define MVPP2_AGGR_TXQ_SIZE 256
  375. /* Descriptor aligned size */
  376. #define MVPP2_DESC_ALIGNED_SIZE 32
  377. /* Descriptor alignment mask */
  378. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  379. /* RX FIFO constants */
  380. #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
  381. #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
  382. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  383. /* RX buffer constants */
  384. #define MVPP2_SKB_SHINFO_SIZE \
  385. 0
  386. #define MVPP2_RX_PKT_SIZE(mtu) \
  387. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  388. ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
  389. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  390. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  391. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  392. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  393. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  394. /* IPv6 max L3 address size */
  395. #define MVPP2_MAX_L3_ADDR_SIZE 16
  396. /* Port flags */
  397. #define MVPP2_F_LOOPBACK BIT(0)
  398. /* Marvell tag types */
  399. enum mvpp2_tag_type {
  400. MVPP2_TAG_TYPE_NONE = 0,
  401. MVPP2_TAG_TYPE_MH = 1,
  402. MVPP2_TAG_TYPE_DSA = 2,
  403. MVPP2_TAG_TYPE_EDSA = 3,
  404. MVPP2_TAG_TYPE_VLAN = 4,
  405. MVPP2_TAG_TYPE_LAST = 5
  406. };
  407. /* Parser constants */
  408. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  409. #define MVPP2_PRS_TCAM_WORDS 6
  410. #define MVPP2_PRS_SRAM_WORDS 4
  411. #define MVPP2_PRS_FLOW_ID_SIZE 64
  412. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  413. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  414. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  415. #define MVPP2_PRS_IPV4_HEAD 0x40
  416. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  417. #define MVPP2_PRS_IPV4_MC 0xe0
  418. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  419. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  420. #define MVPP2_PRS_IPV4_IHL 0x5
  421. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  422. #define MVPP2_PRS_IPV6_MC 0xff
  423. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  424. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  425. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  426. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  427. #define MVPP2_PRS_DBL_VLANS_MAX 100
  428. /* Tcam structure:
  429. * - lookup ID - 4 bits
  430. * - port ID - 1 byte
  431. * - additional information - 1 byte
  432. * - header data - 8 bytes
  433. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  434. */
  435. #define MVPP2_PRS_AI_BITS 8
  436. #define MVPP2_PRS_PORT_MASK 0xff
  437. #define MVPP2_PRS_LU_MASK 0xf
  438. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  439. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  440. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  441. (((offs) * 2) - ((offs) % 2) + 2)
  442. #define MVPP2_PRS_TCAM_AI_BYTE 16
  443. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  444. #define MVPP2_PRS_TCAM_LU_BYTE 20
  445. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  446. #define MVPP2_PRS_TCAM_INV_WORD 5
  447. /* Tcam entries ID */
  448. #define MVPP2_PE_DROP_ALL 0
  449. #define MVPP2_PE_FIRST_FREE_TID 1
  450. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  451. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  452. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  453. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  454. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  455. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  456. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  457. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  458. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  459. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  460. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  461. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  462. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  463. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  464. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  465. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  466. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  467. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  468. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  469. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  470. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  471. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  472. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  473. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  474. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  475. /* Sram structure
  476. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  477. */
  478. #define MVPP2_PRS_SRAM_RI_OFFS 0
  479. #define MVPP2_PRS_SRAM_RI_WORD 0
  480. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  481. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  482. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  483. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  484. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  485. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  486. #define MVPP2_PRS_SRAM_UDF_BITS 8
  487. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  488. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  489. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  490. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  491. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  492. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  493. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  494. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  495. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  496. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  497. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  498. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  499. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  500. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  501. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  502. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  503. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  504. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  505. #define MVPP2_PRS_SRAM_AI_OFFS 90
  506. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  507. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  508. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  509. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  510. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  511. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  512. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  513. /* Sram result info bits assignment */
  514. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  515. #define MVPP2_PRS_RI_DSA_MASK 0x2
  516. #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
  517. #define MVPP2_PRS_RI_VLAN_NONE 0x0
  518. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  519. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  520. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  521. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  522. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  523. #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
  524. #define MVPP2_PRS_RI_L2_UCAST 0x0
  525. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  526. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  527. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  528. #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
  529. #define MVPP2_PRS_RI_L3_UN 0x0
  530. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  531. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  532. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  533. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  534. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  535. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  536. #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
  537. #define MVPP2_PRS_RI_L3_UCAST 0x0
  538. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  539. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  540. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  541. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  542. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  543. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  544. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  545. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  546. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  547. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  548. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  549. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  550. /* Sram additional info bits assignment */
  551. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  552. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  553. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  554. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  555. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  556. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  557. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  558. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  559. /* DSA/EDSA type */
  560. #define MVPP2_PRS_TAGGED true
  561. #define MVPP2_PRS_UNTAGGED false
  562. #define MVPP2_PRS_EDSA true
  563. #define MVPP2_PRS_DSA false
  564. /* MAC entries, shadow udf */
  565. enum mvpp2_prs_udf {
  566. MVPP2_PRS_UDF_MAC_DEF,
  567. MVPP2_PRS_UDF_MAC_RANGE,
  568. MVPP2_PRS_UDF_L2_DEF,
  569. MVPP2_PRS_UDF_L2_DEF_COPY,
  570. MVPP2_PRS_UDF_L2_USER,
  571. };
  572. /* Lookup ID */
  573. enum mvpp2_prs_lookup {
  574. MVPP2_PRS_LU_MH,
  575. MVPP2_PRS_LU_MAC,
  576. MVPP2_PRS_LU_DSA,
  577. MVPP2_PRS_LU_VLAN,
  578. MVPP2_PRS_LU_L2,
  579. MVPP2_PRS_LU_PPPOE,
  580. MVPP2_PRS_LU_IP4,
  581. MVPP2_PRS_LU_IP6,
  582. MVPP2_PRS_LU_FLOWS,
  583. MVPP2_PRS_LU_LAST,
  584. };
  585. /* L3 cast enum */
  586. enum mvpp2_prs_l3_cast {
  587. MVPP2_PRS_L3_UNI_CAST,
  588. MVPP2_PRS_L3_MULTI_CAST,
  589. MVPP2_PRS_L3_BROAD_CAST
  590. };
  591. /* Classifier constants */
  592. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  593. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  594. #define MVPP2_CLS_LKP_TBL_SIZE 64
  595. /* BM constants */
  596. #define MVPP2_BM_POOLS_NUM 1
  597. #define MVPP2_BM_LONG_BUF_NUM 16
  598. #define MVPP2_BM_SHORT_BUF_NUM 16
  599. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  600. #define MVPP2_BM_POOL_PTR_ALIGN 128
  601. #define MVPP2_BM_SWF_LONG_POOL(port) 0
  602. /* BM cookie (32 bits) definition */
  603. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  604. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  605. /* BM short pool packet size
  606. * These value assure that for SWF the total number
  607. * of bytes allocated for each buffer will be 512
  608. */
  609. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  610. enum mvpp2_bm_type {
  611. MVPP2_BM_FREE,
  612. MVPP2_BM_SWF_LONG,
  613. MVPP2_BM_SWF_SHORT
  614. };
  615. /* Definitions */
  616. /* Shared Packet Processor resources */
  617. struct mvpp2 {
  618. /* Shared registers' base addresses */
  619. void __iomem *base;
  620. void __iomem *lms_base;
  621. /* List of pointers to port structures */
  622. struct mvpp2_port **port_list;
  623. /* Aggregated TXQs */
  624. struct mvpp2_tx_queue *aggr_txqs;
  625. /* BM pools */
  626. struct mvpp2_bm_pool *bm_pools;
  627. /* PRS shadow table */
  628. struct mvpp2_prs_shadow *prs_shadow;
  629. /* PRS auxiliary table for double vlan entries control */
  630. bool *prs_double_vlans;
  631. /* Tclk value */
  632. u32 tclk;
  633. struct mii_dev *bus;
  634. };
  635. struct mvpp2_pcpu_stats {
  636. u64 rx_packets;
  637. u64 rx_bytes;
  638. u64 tx_packets;
  639. u64 tx_bytes;
  640. };
  641. struct mvpp2_port {
  642. u8 id;
  643. int irq;
  644. struct mvpp2 *priv;
  645. /* Per-port registers' base address */
  646. void __iomem *base;
  647. struct mvpp2_rx_queue **rxqs;
  648. struct mvpp2_tx_queue **txqs;
  649. int pkt_size;
  650. u32 pending_cause_rx;
  651. /* Per-CPU port control */
  652. struct mvpp2_port_pcpu __percpu *pcpu;
  653. /* Flags */
  654. unsigned long flags;
  655. u16 tx_ring_size;
  656. u16 rx_ring_size;
  657. struct mvpp2_pcpu_stats __percpu *stats;
  658. struct phy_device *phy_dev;
  659. phy_interface_t phy_interface;
  660. int phy_node;
  661. int phyaddr;
  662. int init;
  663. unsigned int link;
  664. unsigned int duplex;
  665. unsigned int speed;
  666. struct mvpp2_bm_pool *pool_long;
  667. struct mvpp2_bm_pool *pool_short;
  668. /* Index of first port's physical RXQ */
  669. u8 first_rxq;
  670. u8 dev_addr[ETH_ALEN];
  671. };
  672. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  673. * layout of the transmit and reception DMA descriptors, and their
  674. * layout is therefore defined by the hardware design
  675. */
  676. #define MVPP2_TXD_L3_OFF_SHIFT 0
  677. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  678. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  679. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  680. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  681. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  682. #define MVPP2_TXD_L4_UDP BIT(24)
  683. #define MVPP2_TXD_L3_IP6 BIT(26)
  684. #define MVPP2_TXD_L_DESC BIT(28)
  685. #define MVPP2_TXD_F_DESC BIT(29)
  686. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  687. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  688. #define MVPP2_RXD_ERR_CRC 0x0
  689. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  690. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  691. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  692. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  693. #define MVPP2_RXD_HWF_SYNC BIT(21)
  694. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  695. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  696. #define MVPP2_RXD_L4_TCP BIT(25)
  697. #define MVPP2_RXD_L4_UDP BIT(26)
  698. #define MVPP2_RXD_L3_IP4 BIT(28)
  699. #define MVPP2_RXD_L3_IP6 BIT(30)
  700. #define MVPP2_RXD_BUF_HDR BIT(31)
  701. struct mvpp2_tx_desc {
  702. u32 command; /* Options used by HW for packet transmitting.*/
  703. u8 packet_offset; /* the offset from the buffer beginning */
  704. u8 phys_txq; /* destination queue ID */
  705. u16 data_size; /* data size of transmitted packet in bytes */
  706. u32 buf_phys_addr; /* physical addr of transmitted buffer */
  707. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  708. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  709. u32 reserved2; /* reserved (for future use) */
  710. };
  711. struct mvpp2_rx_desc {
  712. u32 status; /* info about received packet */
  713. u16 reserved1; /* parser_info (for future use, PnC) */
  714. u16 data_size; /* size of received packet in bytes */
  715. u32 buf_phys_addr; /* physical address of the buffer */
  716. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  717. u16 reserved2; /* gem_port_id (for future use, PON) */
  718. u16 reserved3; /* csum_l4 (for future use, PnC) */
  719. u8 reserved4; /* bm_qset (for future use, BM) */
  720. u8 reserved5;
  721. u16 reserved6; /* classify_info (for future use, PnC) */
  722. u32 reserved7; /* flow_id (for future use, PnC) */
  723. u32 reserved8;
  724. };
  725. /* Per-CPU Tx queue control */
  726. struct mvpp2_txq_pcpu {
  727. int cpu;
  728. /* Number of Tx DMA descriptors in the descriptor ring */
  729. int size;
  730. /* Number of currently used Tx DMA descriptor in the
  731. * descriptor ring
  732. */
  733. int count;
  734. /* Number of Tx DMA descriptors reserved for each CPU */
  735. int reserved_num;
  736. /* Index of last TX DMA descriptor that was inserted */
  737. int txq_put_index;
  738. /* Index of the TX DMA descriptor to be cleaned up */
  739. int txq_get_index;
  740. };
  741. struct mvpp2_tx_queue {
  742. /* Physical number of this Tx queue */
  743. u8 id;
  744. /* Logical number of this Tx queue */
  745. u8 log_id;
  746. /* Number of Tx DMA descriptors in the descriptor ring */
  747. int size;
  748. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  749. int count;
  750. /* Per-CPU control of physical Tx queues */
  751. struct mvpp2_txq_pcpu __percpu *pcpu;
  752. u32 done_pkts_coal;
  753. /* Virtual address of thex Tx DMA descriptors array */
  754. struct mvpp2_tx_desc *descs;
  755. /* DMA address of the Tx DMA descriptors array */
  756. dma_addr_t descs_phys;
  757. /* Index of the last Tx DMA descriptor */
  758. int last_desc;
  759. /* Index of the next Tx DMA descriptor to process */
  760. int next_desc_to_proc;
  761. };
  762. struct mvpp2_rx_queue {
  763. /* RX queue number, in the range 0-31 for physical RXQs */
  764. u8 id;
  765. /* Num of rx descriptors in the rx descriptor ring */
  766. int size;
  767. u32 pkts_coal;
  768. u32 time_coal;
  769. /* Virtual address of the RX DMA descriptors array */
  770. struct mvpp2_rx_desc *descs;
  771. /* DMA address of the RX DMA descriptors array */
  772. dma_addr_t descs_phys;
  773. /* Index of the last RX DMA descriptor */
  774. int last_desc;
  775. /* Index of the next RX DMA descriptor to process */
  776. int next_desc_to_proc;
  777. /* ID of port to which physical RXQ is mapped */
  778. int port;
  779. /* Port's logic RXQ number to which physical RXQ is mapped */
  780. int logic_rxq;
  781. };
  782. union mvpp2_prs_tcam_entry {
  783. u32 word[MVPP2_PRS_TCAM_WORDS];
  784. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  785. };
  786. union mvpp2_prs_sram_entry {
  787. u32 word[MVPP2_PRS_SRAM_WORDS];
  788. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  789. };
  790. struct mvpp2_prs_entry {
  791. u32 index;
  792. union mvpp2_prs_tcam_entry tcam;
  793. union mvpp2_prs_sram_entry sram;
  794. };
  795. struct mvpp2_prs_shadow {
  796. bool valid;
  797. bool finish;
  798. /* Lookup ID */
  799. int lu;
  800. /* User defined offset */
  801. int udf;
  802. /* Result info */
  803. u32 ri;
  804. u32 ri_mask;
  805. };
  806. struct mvpp2_cls_flow_entry {
  807. u32 index;
  808. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  809. };
  810. struct mvpp2_cls_lookup_entry {
  811. u32 lkpid;
  812. u32 way;
  813. u32 data;
  814. };
  815. struct mvpp2_bm_pool {
  816. /* Pool number in the range 0-7 */
  817. int id;
  818. enum mvpp2_bm_type type;
  819. /* Buffer Pointers Pool External (BPPE) size */
  820. int size;
  821. /* Number of buffers for this pool */
  822. int buf_num;
  823. /* Pool buffer size */
  824. int buf_size;
  825. /* Packet size */
  826. int pkt_size;
  827. /* BPPE virtual base address */
  828. u32 *virt_addr;
  829. /* BPPE physical base address */
  830. dma_addr_t phys_addr;
  831. /* Ports using BM pool */
  832. u32 port_map;
  833. /* Occupied buffers indicator */
  834. int in_use_thresh;
  835. };
  836. struct mvpp2_buff_hdr {
  837. u32 next_buff_phys_addr;
  838. u32 next_buff_virt_addr;
  839. u16 byte_count;
  840. u16 info;
  841. u8 reserved1; /* bm_qset (for future use, BM) */
  842. };
  843. /* Buffer header info bits */
  844. #define MVPP2_B_HDR_INFO_MC_ID_MASK 0xfff
  845. #define MVPP2_B_HDR_INFO_MC_ID(info) ((info) & MVPP2_B_HDR_INFO_MC_ID_MASK)
  846. #define MVPP2_B_HDR_INFO_LAST_OFFS 12
  847. #define MVPP2_B_HDR_INFO_LAST_MASK BIT(12)
  848. #define MVPP2_B_HDR_INFO_IS_LAST(info) \
  849. ((info & MVPP2_B_HDR_INFO_LAST_MASK) >> MVPP2_B_HDR_INFO_LAST_OFFS)
  850. /* Static declaractions */
  851. /* Number of RXQs used by single port */
  852. static int rxq_number = MVPP2_DEFAULT_RXQ;
  853. /* Number of TXQs used by single port */
  854. static int txq_number = MVPP2_DEFAULT_TXQ;
  855. #define MVPP2_DRIVER_NAME "mvpp2"
  856. #define MVPP2_DRIVER_VERSION "1.0"
  857. /*
  858. * U-Boot internal data, mostly uncached buffers for descriptors and data
  859. */
  860. struct buffer_location {
  861. struct mvpp2_tx_desc *aggr_tx_descs;
  862. struct mvpp2_tx_desc *tx_descs;
  863. struct mvpp2_rx_desc *rx_descs;
  864. u32 *bm_pool[MVPP2_BM_POOLS_NUM];
  865. u32 *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
  866. int first_rxq;
  867. };
  868. /*
  869. * All 4 interfaces use the same global buffer, since only one interface
  870. * can be enabled at once
  871. */
  872. static struct buffer_location buffer_loc;
  873. /*
  874. * Page table entries are set to 1MB, or multiples of 1MB
  875. * (not < 1MB). driver uses less bd's so use 1MB bdspace.
  876. */
  877. #define BD_SPACE (1 << 20)
  878. /* Utility/helper methods */
  879. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  880. {
  881. writel(data, priv->base + offset);
  882. }
  883. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  884. {
  885. return readl(priv->base + offset);
  886. }
  887. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  888. {
  889. txq_pcpu->txq_get_index++;
  890. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  891. txq_pcpu->txq_get_index = 0;
  892. }
  893. /* Get number of physical egress port */
  894. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  895. {
  896. return MVPP2_MAX_TCONT + port->id;
  897. }
  898. /* Get number of physical TXQ */
  899. static inline int mvpp2_txq_phys(int port, int txq)
  900. {
  901. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  902. }
  903. /* Parser configuration routines */
  904. /* Update parser tcam and sram hw entries */
  905. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  906. {
  907. int i;
  908. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  909. return -EINVAL;
  910. /* Clear entry invalidation bit */
  911. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  912. /* Write tcam index - indirect access */
  913. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  914. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  915. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  916. /* Write sram index - indirect access */
  917. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  918. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  919. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  920. return 0;
  921. }
  922. /* Read tcam entry from hw */
  923. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  924. {
  925. int i;
  926. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  927. return -EINVAL;
  928. /* Write tcam index - indirect access */
  929. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  930. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  931. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  932. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  933. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  934. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  935. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  936. /* Write sram index - indirect access */
  937. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  938. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  939. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  940. return 0;
  941. }
  942. /* Invalidate tcam hw entry */
  943. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  944. {
  945. /* Write index - indirect access */
  946. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  947. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  948. MVPP2_PRS_TCAM_INV_MASK);
  949. }
  950. /* Enable shadow table entry and set its lookup ID */
  951. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  952. {
  953. priv->prs_shadow[index].valid = true;
  954. priv->prs_shadow[index].lu = lu;
  955. }
  956. /* Update ri fields in shadow table entry */
  957. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  958. unsigned int ri, unsigned int ri_mask)
  959. {
  960. priv->prs_shadow[index].ri_mask = ri_mask;
  961. priv->prs_shadow[index].ri = ri;
  962. }
  963. /* Update lookup field in tcam sw entry */
  964. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  965. {
  966. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  967. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  968. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  969. }
  970. /* Update mask for single port in tcam sw entry */
  971. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  972. unsigned int port, bool add)
  973. {
  974. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  975. if (add)
  976. pe->tcam.byte[enable_off] &= ~(1 << port);
  977. else
  978. pe->tcam.byte[enable_off] |= 1 << port;
  979. }
  980. /* Update port map in tcam sw entry */
  981. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  982. unsigned int ports)
  983. {
  984. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  985. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  986. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  987. pe->tcam.byte[enable_off] &= ~port_mask;
  988. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  989. }
  990. /* Obtain port map from tcam sw entry */
  991. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  992. {
  993. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  994. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  995. }
  996. /* Set byte of data and its enable bits in tcam sw entry */
  997. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  998. unsigned int offs, unsigned char byte,
  999. unsigned char enable)
  1000. {
  1001. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1002. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1003. }
  1004. /* Get byte of data and its enable bits from tcam sw entry */
  1005. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1006. unsigned int offs, unsigned char *byte,
  1007. unsigned char *enable)
  1008. {
  1009. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1010. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1011. }
  1012. /* Set ethertype in tcam sw entry */
  1013. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1014. unsigned short ethertype)
  1015. {
  1016. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1017. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1018. }
  1019. /* Set bits in sram sw entry */
  1020. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1021. int val)
  1022. {
  1023. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1024. }
  1025. /* Clear bits in sram sw entry */
  1026. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1027. int val)
  1028. {
  1029. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1030. }
  1031. /* Update ri bits in sram sw entry */
  1032. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1033. unsigned int bits, unsigned int mask)
  1034. {
  1035. unsigned int i;
  1036. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1037. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1038. if (!(mask & BIT(i)))
  1039. continue;
  1040. if (bits & BIT(i))
  1041. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1042. else
  1043. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1044. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1045. }
  1046. }
  1047. /* Update ai bits in sram sw entry */
  1048. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1049. unsigned int bits, unsigned int mask)
  1050. {
  1051. unsigned int i;
  1052. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1053. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1054. if (!(mask & BIT(i)))
  1055. continue;
  1056. if (bits & BIT(i))
  1057. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1058. else
  1059. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1060. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1061. }
  1062. }
  1063. /* Read ai bits from sram sw entry */
  1064. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1065. {
  1066. u8 bits;
  1067. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1068. int ai_en_off = ai_off + 1;
  1069. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1070. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1071. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1072. return bits;
  1073. }
  1074. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1075. * lookup interation
  1076. */
  1077. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1078. unsigned int lu)
  1079. {
  1080. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1081. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1082. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1083. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1084. }
  1085. /* In the sram sw entry set sign and value of the next lookup offset
  1086. * and the offset value generated to the classifier
  1087. */
  1088. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1089. unsigned int op)
  1090. {
  1091. /* Set sign */
  1092. if (shift < 0) {
  1093. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1094. shift = 0 - shift;
  1095. } else {
  1096. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1097. }
  1098. /* Set value */
  1099. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1100. (unsigned char)shift;
  1101. /* Reset and set operation */
  1102. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1103. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1104. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1105. /* Set base offset as current */
  1106. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1107. }
  1108. /* In the sram sw entry set sign and value of the user defined offset
  1109. * generated to the classifier
  1110. */
  1111. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1112. unsigned int type, int offset,
  1113. unsigned int op)
  1114. {
  1115. /* Set sign */
  1116. if (offset < 0) {
  1117. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1118. offset = 0 - offset;
  1119. } else {
  1120. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1121. }
  1122. /* Set value */
  1123. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1124. MVPP2_PRS_SRAM_UDF_MASK);
  1125. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1126. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1127. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1128. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1129. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1130. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1131. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1132. /* Set offset type */
  1133. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1134. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1135. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1136. /* Set offset operation */
  1137. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1138. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1139. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1140. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1141. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1142. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1143. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1144. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1145. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1146. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1147. /* Set base offset as current */
  1148. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1149. }
  1150. /* Find parser flow entry */
  1151. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1152. {
  1153. struct mvpp2_prs_entry *pe;
  1154. int tid;
  1155. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1156. if (!pe)
  1157. return NULL;
  1158. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1159. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1160. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1161. u8 bits;
  1162. if (!priv->prs_shadow[tid].valid ||
  1163. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1164. continue;
  1165. pe->index = tid;
  1166. mvpp2_prs_hw_read(priv, pe);
  1167. bits = mvpp2_prs_sram_ai_get(pe);
  1168. /* Sram store classification lookup ID in AI bits [5:0] */
  1169. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1170. return pe;
  1171. }
  1172. kfree(pe);
  1173. return NULL;
  1174. }
  1175. /* Return first free tcam index, seeking from start to end */
  1176. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1177. unsigned char end)
  1178. {
  1179. int tid;
  1180. if (start > end)
  1181. swap(start, end);
  1182. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1183. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1184. for (tid = start; tid <= end; tid++) {
  1185. if (!priv->prs_shadow[tid].valid)
  1186. return tid;
  1187. }
  1188. return -EINVAL;
  1189. }
  1190. /* Enable/disable dropping all mac da's */
  1191. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1192. {
  1193. struct mvpp2_prs_entry pe;
  1194. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1195. /* Entry exist - update port only */
  1196. pe.index = MVPP2_PE_DROP_ALL;
  1197. mvpp2_prs_hw_read(priv, &pe);
  1198. } else {
  1199. /* Entry doesn't exist - create new */
  1200. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1201. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1202. pe.index = MVPP2_PE_DROP_ALL;
  1203. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1204. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1205. MVPP2_PRS_RI_DROP_MASK);
  1206. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1207. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1208. /* Update shadow table */
  1209. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1210. /* Mask all ports */
  1211. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1212. }
  1213. /* Update port mask */
  1214. mvpp2_prs_tcam_port_set(&pe, port, add);
  1215. mvpp2_prs_hw_write(priv, &pe);
  1216. }
  1217. /* Set port to promiscuous mode */
  1218. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1219. {
  1220. struct mvpp2_prs_entry pe;
  1221. /* Promiscuous mode - Accept unknown packets */
  1222. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1223. /* Entry exist - update port only */
  1224. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1225. mvpp2_prs_hw_read(priv, &pe);
  1226. } else {
  1227. /* Entry doesn't exist - create new */
  1228. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1229. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1230. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1231. /* Continue - set next lookup */
  1232. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1233. /* Set result info bits */
  1234. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1235. MVPP2_PRS_RI_L2_CAST_MASK);
  1236. /* Shift to ethertype */
  1237. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1238. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1239. /* Mask all ports */
  1240. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1241. /* Update shadow table */
  1242. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1243. }
  1244. /* Update port mask */
  1245. mvpp2_prs_tcam_port_set(&pe, port, add);
  1246. mvpp2_prs_hw_write(priv, &pe);
  1247. }
  1248. /* Accept multicast */
  1249. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1250. bool add)
  1251. {
  1252. struct mvpp2_prs_entry pe;
  1253. unsigned char da_mc;
  1254. /* Ethernet multicast address first byte is
  1255. * 0x01 for IPv4 and 0x33 for IPv6
  1256. */
  1257. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1258. if (priv->prs_shadow[index].valid) {
  1259. /* Entry exist - update port only */
  1260. pe.index = index;
  1261. mvpp2_prs_hw_read(priv, &pe);
  1262. } else {
  1263. /* Entry doesn't exist - create new */
  1264. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1265. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1266. pe.index = index;
  1267. /* Continue - set next lookup */
  1268. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1269. /* Set result info bits */
  1270. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1271. MVPP2_PRS_RI_L2_CAST_MASK);
  1272. /* Update tcam entry data first byte */
  1273. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1274. /* Shift to ethertype */
  1275. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1276. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1277. /* Mask all ports */
  1278. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1279. /* Update shadow table */
  1280. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1281. }
  1282. /* Update port mask */
  1283. mvpp2_prs_tcam_port_set(&pe, port, add);
  1284. mvpp2_prs_hw_write(priv, &pe);
  1285. }
  1286. /* Parser per-port initialization */
  1287. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1288. int lu_max, int offset)
  1289. {
  1290. u32 val;
  1291. /* Set lookup ID */
  1292. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1293. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1294. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1295. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1296. /* Set maximum number of loops for packet received from port */
  1297. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1298. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1299. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1300. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1301. /* Set initial offset for packet header extraction for the first
  1302. * searching loop
  1303. */
  1304. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1305. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1306. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1307. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1308. }
  1309. /* Default flow entries initialization for all ports */
  1310. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1311. {
  1312. struct mvpp2_prs_entry pe;
  1313. int port;
  1314. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1315. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1316. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1317. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1318. /* Mask all ports */
  1319. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1320. /* Set flow ID*/
  1321. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1322. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1323. /* Update shadow table and hw entry */
  1324. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1325. mvpp2_prs_hw_write(priv, &pe);
  1326. }
  1327. }
  1328. /* Set default entry for Marvell Header field */
  1329. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1330. {
  1331. struct mvpp2_prs_entry pe;
  1332. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1333. pe.index = MVPP2_PE_MH_DEFAULT;
  1334. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1335. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1336. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1337. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1338. /* Unmask all ports */
  1339. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1340. /* Update shadow table and hw entry */
  1341. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1342. mvpp2_prs_hw_write(priv, &pe);
  1343. }
  1344. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1345. * multicast MAC addresses
  1346. */
  1347. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1348. {
  1349. struct mvpp2_prs_entry pe;
  1350. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1351. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1352. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1353. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1354. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1355. MVPP2_PRS_RI_DROP_MASK);
  1356. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1357. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1358. /* Unmask all ports */
  1359. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1360. /* Update shadow table and hw entry */
  1361. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1362. mvpp2_prs_hw_write(priv, &pe);
  1363. /* place holders only - no ports */
  1364. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1365. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1366. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1367. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1368. }
  1369. /* Match basic ethertypes */
  1370. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1371. {
  1372. struct mvpp2_prs_entry pe;
  1373. int tid;
  1374. /* Ethertype: PPPoE */
  1375. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1376. MVPP2_PE_LAST_FREE_TID);
  1377. if (tid < 0)
  1378. return tid;
  1379. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1380. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1381. pe.index = tid;
  1382. mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
  1383. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1384. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1385. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1386. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1387. MVPP2_PRS_RI_PPPOE_MASK);
  1388. /* Update shadow table and hw entry */
  1389. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1390. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1391. priv->prs_shadow[pe.index].finish = false;
  1392. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1393. MVPP2_PRS_RI_PPPOE_MASK);
  1394. mvpp2_prs_hw_write(priv, &pe);
  1395. /* Ethertype: ARP */
  1396. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1397. MVPP2_PE_LAST_FREE_TID);
  1398. if (tid < 0)
  1399. return tid;
  1400. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1401. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1402. pe.index = tid;
  1403. mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
  1404. /* Generate flow in the next iteration*/
  1405. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1406. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1407. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1408. MVPP2_PRS_RI_L3_PROTO_MASK);
  1409. /* Set L3 offset */
  1410. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1411. MVPP2_ETH_TYPE_LEN,
  1412. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1413. /* Update shadow table and hw entry */
  1414. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1415. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1416. priv->prs_shadow[pe.index].finish = true;
  1417. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1418. MVPP2_PRS_RI_L3_PROTO_MASK);
  1419. mvpp2_prs_hw_write(priv, &pe);
  1420. /* Ethertype: LBTD */
  1421. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1422. MVPP2_PE_LAST_FREE_TID);
  1423. if (tid < 0)
  1424. return tid;
  1425. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1426. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1427. pe.index = tid;
  1428. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1429. /* Generate flow in the next iteration*/
  1430. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1431. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1432. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1433. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1434. MVPP2_PRS_RI_CPU_CODE_MASK |
  1435. MVPP2_PRS_RI_UDF3_MASK);
  1436. /* Set L3 offset */
  1437. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1438. MVPP2_ETH_TYPE_LEN,
  1439. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1440. /* Update shadow table and hw entry */
  1441. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1442. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1443. priv->prs_shadow[pe.index].finish = true;
  1444. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1445. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1446. MVPP2_PRS_RI_CPU_CODE_MASK |
  1447. MVPP2_PRS_RI_UDF3_MASK);
  1448. mvpp2_prs_hw_write(priv, &pe);
  1449. /* Ethertype: IPv4 without options */
  1450. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1451. MVPP2_PE_LAST_FREE_TID);
  1452. if (tid < 0)
  1453. return tid;
  1454. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1455. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1456. pe.index = tid;
  1457. mvpp2_prs_match_etype(&pe, 0, PROT_IP);
  1458. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1459. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1460. MVPP2_PRS_IPV4_HEAD_MASK |
  1461. MVPP2_PRS_IPV4_IHL_MASK);
  1462. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1463. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1464. MVPP2_PRS_RI_L3_PROTO_MASK);
  1465. /* Skip eth_type + 4 bytes of IP header */
  1466. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1467. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1468. /* Set L3 offset */
  1469. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1470. MVPP2_ETH_TYPE_LEN,
  1471. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1472. /* Update shadow table and hw entry */
  1473. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1474. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1475. priv->prs_shadow[pe.index].finish = false;
  1476. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1477. MVPP2_PRS_RI_L3_PROTO_MASK);
  1478. mvpp2_prs_hw_write(priv, &pe);
  1479. /* Ethertype: IPv4 with options */
  1480. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1481. MVPP2_PE_LAST_FREE_TID);
  1482. if (tid < 0)
  1483. return tid;
  1484. pe.index = tid;
  1485. /* Clear tcam data before updating */
  1486. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1487. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1488. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1489. MVPP2_PRS_IPV4_HEAD,
  1490. MVPP2_PRS_IPV4_HEAD_MASK);
  1491. /* Clear ri before updating */
  1492. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1493. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1494. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  1495. MVPP2_PRS_RI_L3_PROTO_MASK);
  1496. /* Update shadow table and hw entry */
  1497. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1498. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1499. priv->prs_shadow[pe.index].finish = false;
  1500. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  1501. MVPP2_PRS_RI_L3_PROTO_MASK);
  1502. mvpp2_prs_hw_write(priv, &pe);
  1503. /* Ethertype: IPv6 without options */
  1504. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1505. MVPP2_PE_LAST_FREE_TID);
  1506. if (tid < 0)
  1507. return tid;
  1508. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1509. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1510. pe.index = tid;
  1511. mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
  1512. /* Skip DIP of IPV6 header */
  1513. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  1514. MVPP2_MAX_L3_ADDR_SIZE,
  1515. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1516. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1517. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  1518. MVPP2_PRS_RI_L3_PROTO_MASK);
  1519. /* Set L3 offset */
  1520. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1521. MVPP2_ETH_TYPE_LEN,
  1522. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1523. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1524. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1525. priv->prs_shadow[pe.index].finish = false;
  1526. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  1527. MVPP2_PRS_RI_L3_PROTO_MASK);
  1528. mvpp2_prs_hw_write(priv, &pe);
  1529. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  1530. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1531. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1532. pe.index = MVPP2_PE_ETH_TYPE_UN;
  1533. /* Unmask all ports */
  1534. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1535. /* Generate flow in the next iteration*/
  1536. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1537. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1538. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  1539. MVPP2_PRS_RI_L3_PROTO_MASK);
  1540. /* Set L3 offset even it's unknown L3 */
  1541. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1542. MVPP2_ETH_TYPE_LEN,
  1543. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1544. /* Update shadow table and hw entry */
  1545. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1546. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1547. priv->prs_shadow[pe.index].finish = true;
  1548. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  1549. MVPP2_PRS_RI_L3_PROTO_MASK);
  1550. mvpp2_prs_hw_write(priv, &pe);
  1551. return 0;
  1552. }
  1553. /* Parser default initialization */
  1554. static int mvpp2_prs_default_init(struct udevice *dev,
  1555. struct mvpp2 *priv)
  1556. {
  1557. int err, index, i;
  1558. /* Enable tcam table */
  1559. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  1560. /* Clear all tcam and sram entries */
  1561. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  1562. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1563. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1564. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  1565. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  1566. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1567. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  1568. }
  1569. /* Invalidate all tcam entries */
  1570. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  1571. mvpp2_prs_hw_inv(priv, index);
  1572. priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  1573. sizeof(struct mvpp2_prs_shadow),
  1574. GFP_KERNEL);
  1575. if (!priv->prs_shadow)
  1576. return -ENOMEM;
  1577. /* Always start from lookup = 0 */
  1578. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  1579. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  1580. MVPP2_PRS_PORT_LU_MAX, 0);
  1581. mvpp2_prs_def_flow_init(priv);
  1582. mvpp2_prs_mh_init(priv);
  1583. mvpp2_prs_mac_init(priv);
  1584. err = mvpp2_prs_etype_init(priv);
  1585. if (err)
  1586. return err;
  1587. return 0;
  1588. }
  1589. /* Compare MAC DA with tcam entry data */
  1590. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  1591. const u8 *da, unsigned char *mask)
  1592. {
  1593. unsigned char tcam_byte, tcam_mask;
  1594. int index;
  1595. for (index = 0; index < ETH_ALEN; index++) {
  1596. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  1597. if (tcam_mask != mask[index])
  1598. return false;
  1599. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  1600. return false;
  1601. }
  1602. return true;
  1603. }
  1604. /* Find tcam entry with matched pair <MAC DA, port> */
  1605. static struct mvpp2_prs_entry *
  1606. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  1607. unsigned char *mask, int udf_type)
  1608. {
  1609. struct mvpp2_prs_entry *pe;
  1610. int tid;
  1611. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1612. if (!pe)
  1613. return NULL;
  1614. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1615. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  1616. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1617. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1618. unsigned int entry_pmap;
  1619. if (!priv->prs_shadow[tid].valid ||
  1620. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  1621. (priv->prs_shadow[tid].udf != udf_type))
  1622. continue;
  1623. pe->index = tid;
  1624. mvpp2_prs_hw_read(priv, pe);
  1625. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  1626. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  1627. entry_pmap == pmap)
  1628. return pe;
  1629. }
  1630. kfree(pe);
  1631. return NULL;
  1632. }
  1633. /* Update parser's mac da entry */
  1634. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  1635. const u8 *da, bool add)
  1636. {
  1637. struct mvpp2_prs_entry *pe;
  1638. unsigned int pmap, len, ri;
  1639. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1640. int tid;
  1641. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  1642. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  1643. MVPP2_PRS_UDF_MAC_DEF);
  1644. /* No such entry */
  1645. if (!pe) {
  1646. if (!add)
  1647. return 0;
  1648. /* Create new TCAM entry */
  1649. /* Find first range mac entry*/
  1650. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1651. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  1652. if (priv->prs_shadow[tid].valid &&
  1653. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  1654. (priv->prs_shadow[tid].udf ==
  1655. MVPP2_PRS_UDF_MAC_RANGE))
  1656. break;
  1657. /* Go through the all entries from first to last */
  1658. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1659. tid - 1);
  1660. if (tid < 0)
  1661. return tid;
  1662. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1663. if (!pe)
  1664. return -1;
  1665. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1666. pe->index = tid;
  1667. /* Mask all ports */
  1668. mvpp2_prs_tcam_port_map_set(pe, 0);
  1669. }
  1670. /* Update port mask */
  1671. mvpp2_prs_tcam_port_set(pe, port, add);
  1672. /* Invalidate the entry if no ports are left enabled */
  1673. pmap = mvpp2_prs_tcam_port_map_get(pe);
  1674. if (pmap == 0) {
  1675. if (add) {
  1676. kfree(pe);
  1677. return -1;
  1678. }
  1679. mvpp2_prs_hw_inv(priv, pe->index);
  1680. priv->prs_shadow[pe->index].valid = false;
  1681. kfree(pe);
  1682. return 0;
  1683. }
  1684. /* Continue - set next lookup */
  1685. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  1686. /* Set match on DA */
  1687. len = ETH_ALEN;
  1688. while (len--)
  1689. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  1690. /* Set result info bits */
  1691. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  1692. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1693. MVPP2_PRS_RI_MAC_ME_MASK);
  1694. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1695. MVPP2_PRS_RI_MAC_ME_MASK);
  1696. /* Shift to ethertype */
  1697. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  1698. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1699. /* Update shadow table and hw entry */
  1700. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  1701. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  1702. mvpp2_prs_hw_write(priv, pe);
  1703. kfree(pe);
  1704. return 0;
  1705. }
  1706. static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
  1707. {
  1708. int err;
  1709. /* Remove old parser entry */
  1710. err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
  1711. false);
  1712. if (err)
  1713. return err;
  1714. /* Add new parser entry */
  1715. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  1716. if (err)
  1717. return err;
  1718. /* Set addr in the device */
  1719. memcpy(port->dev_addr, da, ETH_ALEN);
  1720. return 0;
  1721. }
  1722. /* Set prs flow for the port */
  1723. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  1724. {
  1725. struct mvpp2_prs_entry *pe;
  1726. int tid;
  1727. pe = mvpp2_prs_flow_find(port->priv, port->id);
  1728. /* Such entry not exist */
  1729. if (!pe) {
  1730. /* Go through the all entires from last to first */
  1731. tid = mvpp2_prs_tcam_first_free(port->priv,
  1732. MVPP2_PE_LAST_FREE_TID,
  1733. MVPP2_PE_FIRST_FREE_TID);
  1734. if (tid < 0)
  1735. return tid;
  1736. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1737. if (!pe)
  1738. return -ENOMEM;
  1739. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1740. pe->index = tid;
  1741. /* Set flow ID*/
  1742. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  1743. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1744. /* Update shadow table */
  1745. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  1746. }
  1747. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  1748. mvpp2_prs_hw_write(port->priv, pe);
  1749. kfree(pe);
  1750. return 0;
  1751. }
  1752. /* Classifier configuration routines */
  1753. /* Update classification flow table registers */
  1754. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  1755. struct mvpp2_cls_flow_entry *fe)
  1756. {
  1757. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  1758. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  1759. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  1760. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  1761. }
  1762. /* Update classification lookup table register */
  1763. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  1764. struct mvpp2_cls_lookup_entry *le)
  1765. {
  1766. u32 val;
  1767. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  1768. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  1769. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  1770. }
  1771. /* Classifier default initialization */
  1772. static void mvpp2_cls_init(struct mvpp2 *priv)
  1773. {
  1774. struct mvpp2_cls_lookup_entry le;
  1775. struct mvpp2_cls_flow_entry fe;
  1776. int index;
  1777. /* Enable classifier */
  1778. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  1779. /* Clear classifier flow table */
  1780. memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
  1781. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  1782. fe.index = index;
  1783. mvpp2_cls_flow_write(priv, &fe);
  1784. }
  1785. /* Clear classifier lookup table */
  1786. le.data = 0;
  1787. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  1788. le.lkpid = index;
  1789. le.way = 0;
  1790. mvpp2_cls_lookup_write(priv, &le);
  1791. le.way = 1;
  1792. mvpp2_cls_lookup_write(priv, &le);
  1793. }
  1794. }
  1795. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  1796. {
  1797. struct mvpp2_cls_lookup_entry le;
  1798. u32 val;
  1799. /* Set way for the port */
  1800. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  1801. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  1802. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  1803. /* Pick the entry to be accessed in lookup ID decoding table
  1804. * according to the way and lkpid.
  1805. */
  1806. le.lkpid = port->id;
  1807. le.way = 0;
  1808. le.data = 0;
  1809. /* Set initial CPU queue for receiving packets */
  1810. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  1811. le.data |= port->first_rxq;
  1812. /* Disable classification engines */
  1813. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  1814. /* Update lookup ID table entry */
  1815. mvpp2_cls_lookup_write(port->priv, &le);
  1816. }
  1817. /* Set CPU queue number for oversize packets */
  1818. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  1819. {
  1820. u32 val;
  1821. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  1822. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  1823. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  1824. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  1825. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  1826. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  1827. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  1828. }
  1829. /* Buffer Manager configuration routines */
  1830. /* Create pool */
  1831. static int mvpp2_bm_pool_create(struct udevice *dev,
  1832. struct mvpp2 *priv,
  1833. struct mvpp2_bm_pool *bm_pool, int size)
  1834. {
  1835. u32 val;
  1836. bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
  1837. bm_pool->phys_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
  1838. if (!bm_pool->virt_addr)
  1839. return -ENOMEM;
  1840. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  1841. MVPP2_BM_POOL_PTR_ALIGN)) {
  1842. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  1843. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  1844. return -ENOMEM;
  1845. }
  1846. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  1847. bm_pool->phys_addr);
  1848. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  1849. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1850. val |= MVPP2_BM_START_MASK;
  1851. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1852. bm_pool->type = MVPP2_BM_FREE;
  1853. bm_pool->size = size;
  1854. bm_pool->pkt_size = 0;
  1855. bm_pool->buf_num = 0;
  1856. return 0;
  1857. }
  1858. /* Set pool buffer size */
  1859. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  1860. struct mvpp2_bm_pool *bm_pool,
  1861. int buf_size)
  1862. {
  1863. u32 val;
  1864. bm_pool->buf_size = buf_size;
  1865. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  1866. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  1867. }
  1868. /* Free all buffers from the pool */
  1869. static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
  1870. struct mvpp2_bm_pool *bm_pool)
  1871. {
  1872. bm_pool->buf_num = 0;
  1873. }
  1874. /* Cleanup pool */
  1875. static int mvpp2_bm_pool_destroy(struct udevice *dev,
  1876. struct mvpp2 *priv,
  1877. struct mvpp2_bm_pool *bm_pool)
  1878. {
  1879. u32 val;
  1880. mvpp2_bm_bufs_free(dev, priv, bm_pool);
  1881. if (bm_pool->buf_num) {
  1882. dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
  1883. return 0;
  1884. }
  1885. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1886. val |= MVPP2_BM_STOP_MASK;
  1887. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1888. return 0;
  1889. }
  1890. static int mvpp2_bm_pools_init(struct udevice *dev,
  1891. struct mvpp2 *priv)
  1892. {
  1893. int i, err, size;
  1894. struct mvpp2_bm_pool *bm_pool;
  1895. /* Create all pools with maximum size */
  1896. size = MVPP2_BM_POOL_SIZE_MAX;
  1897. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1898. bm_pool = &priv->bm_pools[i];
  1899. bm_pool->id = i;
  1900. err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
  1901. if (err)
  1902. goto err_unroll_pools;
  1903. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  1904. }
  1905. return 0;
  1906. err_unroll_pools:
  1907. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  1908. for (i = i - 1; i >= 0; i--)
  1909. mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
  1910. return err;
  1911. }
  1912. static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
  1913. {
  1914. int i, err;
  1915. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1916. /* Mask BM all interrupts */
  1917. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  1918. /* Clear BM cause register */
  1919. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  1920. }
  1921. /* Allocate and initialize BM pools */
  1922. priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
  1923. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  1924. if (!priv->bm_pools)
  1925. return -ENOMEM;
  1926. err = mvpp2_bm_pools_init(dev, priv);
  1927. if (err < 0)
  1928. return err;
  1929. return 0;
  1930. }
  1931. /* Attach long pool to rxq */
  1932. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  1933. int lrxq, int long_pool)
  1934. {
  1935. u32 val;
  1936. int prxq;
  1937. /* Get queue physical ID */
  1938. prxq = port->rxqs[lrxq]->id;
  1939. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  1940. val &= ~MVPP2_RXQ_POOL_LONG_MASK;
  1941. val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
  1942. MVPP2_RXQ_POOL_LONG_MASK);
  1943. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  1944. }
  1945. /* Set pool number in a BM cookie */
  1946. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  1947. {
  1948. u32 bm;
  1949. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  1950. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  1951. return bm;
  1952. }
  1953. /* Get pool number from a BM cookie */
  1954. static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
  1955. {
  1956. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  1957. }
  1958. /* Release buffer to BM */
  1959. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  1960. dma_addr_t buf_phys_addr,
  1961. unsigned long buf_virt_addr)
  1962. {
  1963. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_virt_addr);
  1964. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_phys_addr);
  1965. }
  1966. /* Refill BM pool */
  1967. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  1968. u32 phys_addr, u32 cookie)
  1969. {
  1970. int pool = mvpp2_bm_cookie_pool_get(bm);
  1971. mvpp2_bm_pool_put(port, pool, phys_addr, cookie);
  1972. }
  1973. /* Allocate buffers for the pool */
  1974. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  1975. struct mvpp2_bm_pool *bm_pool, int buf_num)
  1976. {
  1977. int i;
  1978. if (buf_num < 0 ||
  1979. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  1980. netdev_err(port->dev,
  1981. "cannot allocate %d buffers for pool %d\n",
  1982. buf_num, bm_pool->id);
  1983. return 0;
  1984. }
  1985. for (i = 0; i < buf_num; i++) {
  1986. mvpp2_bm_pool_put(port, bm_pool->id,
  1987. (dma_addr_t)buffer_loc.rx_buffer[i],
  1988. (unsigned long)buffer_loc.rx_buffer[i]);
  1989. }
  1990. /* Update BM driver with number of buffers added to pool */
  1991. bm_pool->buf_num += i;
  1992. bm_pool->in_use_thresh = bm_pool->buf_num / 4;
  1993. return i;
  1994. }
  1995. /* Notify the driver that BM pool is being used as specific type and return the
  1996. * pool pointer on success
  1997. */
  1998. static struct mvpp2_bm_pool *
  1999. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  2000. int pkt_size)
  2001. {
  2002. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  2003. int num;
  2004. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  2005. netdev_err(port->dev, "mixing pool types is forbidden\n");
  2006. return NULL;
  2007. }
  2008. if (new_pool->type == MVPP2_BM_FREE)
  2009. new_pool->type = type;
  2010. /* Allocate buffers in case BM pool is used as long pool, but packet
  2011. * size doesn't match MTU or BM pool hasn't being used yet
  2012. */
  2013. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  2014. (new_pool->pkt_size == 0)) {
  2015. int pkts_num;
  2016. /* Set default buffer number or free all the buffers in case
  2017. * the pool is not empty
  2018. */
  2019. pkts_num = new_pool->buf_num;
  2020. if (pkts_num == 0)
  2021. pkts_num = type == MVPP2_BM_SWF_LONG ?
  2022. MVPP2_BM_LONG_BUF_NUM :
  2023. MVPP2_BM_SHORT_BUF_NUM;
  2024. else
  2025. mvpp2_bm_bufs_free(NULL,
  2026. port->priv, new_pool);
  2027. new_pool->pkt_size = pkt_size;
  2028. /* Allocate buffers for this pool */
  2029. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  2030. if (num != pkts_num) {
  2031. dev_err(dev, "pool %d: %d of %d allocated\n",
  2032. new_pool->id, num, pkts_num);
  2033. return NULL;
  2034. }
  2035. }
  2036. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  2037. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  2038. return new_pool;
  2039. }
  2040. /* Initialize pools for swf */
  2041. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  2042. {
  2043. int rxq;
  2044. if (!port->pool_long) {
  2045. port->pool_long =
  2046. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  2047. MVPP2_BM_SWF_LONG,
  2048. port->pkt_size);
  2049. if (!port->pool_long)
  2050. return -ENOMEM;
  2051. port->pool_long->port_map |= (1 << port->id);
  2052. for (rxq = 0; rxq < rxq_number; rxq++)
  2053. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  2054. }
  2055. return 0;
  2056. }
  2057. /* Port configuration routines */
  2058. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  2059. {
  2060. u32 val;
  2061. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2062. switch (port->phy_interface) {
  2063. case PHY_INTERFACE_MODE_SGMII:
  2064. val |= MVPP2_GMAC_INBAND_AN_MASK;
  2065. break;
  2066. case PHY_INTERFACE_MODE_RGMII:
  2067. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  2068. default:
  2069. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2070. }
  2071. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2072. }
  2073. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  2074. {
  2075. u32 val;
  2076. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2077. val |= MVPP2_GMAC_FC_ADV_EN;
  2078. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2079. }
  2080. static void mvpp2_port_enable(struct mvpp2_port *port)
  2081. {
  2082. u32 val;
  2083. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2084. val |= MVPP2_GMAC_PORT_EN_MASK;
  2085. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  2086. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2087. }
  2088. static void mvpp2_port_disable(struct mvpp2_port *port)
  2089. {
  2090. u32 val;
  2091. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2092. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  2093. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2094. }
  2095. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  2096. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  2097. {
  2098. u32 val;
  2099. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  2100. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2101. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2102. }
  2103. /* Configure loopback port */
  2104. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  2105. {
  2106. u32 val;
  2107. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2108. if (port->speed == 1000)
  2109. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  2110. else
  2111. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  2112. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2113. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  2114. else
  2115. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  2116. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2117. }
  2118. static void mvpp2_port_reset(struct mvpp2_port *port)
  2119. {
  2120. u32 val;
  2121. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2122. ~MVPP2_GMAC_PORT_RESET_MASK;
  2123. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2124. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2125. MVPP2_GMAC_PORT_RESET_MASK)
  2126. continue;
  2127. }
  2128. /* Change maximum receive size of the port */
  2129. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  2130. {
  2131. u32 val;
  2132. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2133. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2134. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  2135. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  2136. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2137. }
  2138. /* Set defaults to the MVPP2 port */
  2139. static void mvpp2_defaults_set(struct mvpp2_port *port)
  2140. {
  2141. int tx_port_num, val, queue, ptxq, lrxq;
  2142. /* Configure port to loopback if needed */
  2143. if (port->flags & MVPP2_F_LOOPBACK)
  2144. mvpp2_port_loopback_set(port);
  2145. /* Update TX FIFO MIN Threshold */
  2146. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2147. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2148. /* Min. TX threshold must be less than minimal packet length */
  2149. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  2150. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2151. /* Disable Legacy WRR, Disable EJP, Release from reset */
  2152. tx_port_num = mvpp2_egress_port(port);
  2153. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  2154. tx_port_num);
  2155. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  2156. /* Close bandwidth for all queues */
  2157. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  2158. ptxq = mvpp2_txq_phys(port->id, queue);
  2159. mvpp2_write(port->priv,
  2160. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  2161. }
  2162. /* Set refill period to 1 usec, refill tokens
  2163. * and bucket size to maximum
  2164. */
  2165. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
  2166. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  2167. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  2168. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  2169. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  2170. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  2171. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  2172. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2173. /* Set MaximumLowLatencyPacketSize value to 256 */
  2174. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  2175. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  2176. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  2177. /* Enable Rx cache snoop */
  2178. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2179. queue = port->rxqs[lrxq]->id;
  2180. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2181. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  2182. MVPP2_SNOOP_BUF_HDR_MASK;
  2183. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2184. }
  2185. }
  2186. /* Enable/disable receiving packets */
  2187. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  2188. {
  2189. u32 val;
  2190. int lrxq, queue;
  2191. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2192. queue = port->rxqs[lrxq]->id;
  2193. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2194. val &= ~MVPP2_RXQ_DISABLE_MASK;
  2195. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2196. }
  2197. }
  2198. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  2199. {
  2200. u32 val;
  2201. int lrxq, queue;
  2202. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2203. queue = port->rxqs[lrxq]->id;
  2204. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2205. val |= MVPP2_RXQ_DISABLE_MASK;
  2206. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2207. }
  2208. }
  2209. /* Enable transmit via physical egress queue
  2210. * - HW starts take descriptors from DRAM
  2211. */
  2212. static void mvpp2_egress_enable(struct mvpp2_port *port)
  2213. {
  2214. u32 qmap;
  2215. int queue;
  2216. int tx_port_num = mvpp2_egress_port(port);
  2217. /* Enable all initialized TXs. */
  2218. qmap = 0;
  2219. for (queue = 0; queue < txq_number; queue++) {
  2220. struct mvpp2_tx_queue *txq = port->txqs[queue];
  2221. if (txq->descs != NULL)
  2222. qmap |= (1 << queue);
  2223. }
  2224. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2225. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  2226. }
  2227. /* Disable transmit via physical egress queue
  2228. * - HW doesn't take descriptors from DRAM
  2229. */
  2230. static void mvpp2_egress_disable(struct mvpp2_port *port)
  2231. {
  2232. u32 reg_data;
  2233. int delay;
  2234. int tx_port_num = mvpp2_egress_port(port);
  2235. /* Issue stop command for active channels only */
  2236. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2237. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  2238. MVPP2_TXP_SCHED_ENQ_MASK;
  2239. if (reg_data != 0)
  2240. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  2241. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  2242. /* Wait for all Tx activity to terminate. */
  2243. delay = 0;
  2244. do {
  2245. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  2246. netdev_warn(port->dev,
  2247. "Tx stop timed out, status=0x%08x\n",
  2248. reg_data);
  2249. break;
  2250. }
  2251. mdelay(1);
  2252. delay++;
  2253. /* Check port TX Command register that all
  2254. * Tx queues are stopped
  2255. */
  2256. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  2257. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  2258. }
  2259. /* Rx descriptors helper methods */
  2260. /* Get number of Rx descriptors occupied by received packets */
  2261. static inline int
  2262. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  2263. {
  2264. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  2265. return val & MVPP2_RXQ_OCCUPIED_MASK;
  2266. }
  2267. /* Update Rx queue status with the number of occupied and available
  2268. * Rx descriptor slots.
  2269. */
  2270. static inline void
  2271. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  2272. int used_count, int free_count)
  2273. {
  2274. /* Decrement the number of used descriptors and increment count
  2275. * increment the number of free descriptors.
  2276. */
  2277. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  2278. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  2279. }
  2280. /* Get pointer to next RX descriptor to be processed by SW */
  2281. static inline struct mvpp2_rx_desc *
  2282. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  2283. {
  2284. int rx_desc = rxq->next_desc_to_proc;
  2285. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  2286. prefetch(rxq->descs + rxq->next_desc_to_proc);
  2287. return rxq->descs + rx_desc;
  2288. }
  2289. /* Set rx queue offset */
  2290. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  2291. int prxq, int offset)
  2292. {
  2293. u32 val;
  2294. /* Convert offset from bytes to units of 32 bytes */
  2295. offset = offset >> 5;
  2296. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2297. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  2298. /* Offset is in */
  2299. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  2300. MVPP2_RXQ_PACKET_OFFSET_MASK);
  2301. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2302. }
  2303. /* Obtain BM cookie information from descriptor */
  2304. static u32 mvpp2_bm_cookie_build(struct mvpp2_rx_desc *rx_desc)
  2305. {
  2306. int pool = (rx_desc->status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  2307. MVPP2_RXD_BM_POOL_ID_OFFS;
  2308. int cpu = smp_processor_id();
  2309. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  2310. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  2311. }
  2312. /* Tx descriptors helper methods */
  2313. /* Get number of Tx descriptors waiting to be transmitted by HW */
  2314. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  2315. struct mvpp2_tx_queue *txq)
  2316. {
  2317. u32 val;
  2318. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2319. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2320. return val & MVPP2_TXQ_PENDING_MASK;
  2321. }
  2322. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  2323. static struct mvpp2_tx_desc *
  2324. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  2325. {
  2326. int tx_desc = txq->next_desc_to_proc;
  2327. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  2328. return txq->descs + tx_desc;
  2329. }
  2330. /* Update HW with number of aggregated Tx descriptors to be sent */
  2331. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  2332. {
  2333. /* aggregated access - relevant TXQ number is written in TX desc */
  2334. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  2335. }
  2336. /* Get number of sent descriptors and decrement counter.
  2337. * The number of sent descriptors is returned.
  2338. * Per-CPU access
  2339. */
  2340. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  2341. struct mvpp2_tx_queue *txq)
  2342. {
  2343. u32 val;
  2344. /* Reading status reg resets transmitted descriptor counter */
  2345. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  2346. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  2347. MVPP2_TRANSMITTED_COUNT_OFFSET;
  2348. }
  2349. static void mvpp2_txq_sent_counter_clear(void *arg)
  2350. {
  2351. struct mvpp2_port *port = arg;
  2352. int queue;
  2353. for (queue = 0; queue < txq_number; queue++) {
  2354. int id = port->txqs[queue]->id;
  2355. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  2356. }
  2357. }
  2358. /* Set max sizes for Tx queues */
  2359. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  2360. {
  2361. u32 val, size, mtu;
  2362. int txq, tx_port_num;
  2363. mtu = port->pkt_size * 8;
  2364. if (mtu > MVPP2_TXP_MTU_MAX)
  2365. mtu = MVPP2_TXP_MTU_MAX;
  2366. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  2367. mtu = 3 * mtu;
  2368. /* Indirect access to registers */
  2369. tx_port_num = mvpp2_egress_port(port);
  2370. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2371. /* Set MTU */
  2372. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  2373. val &= ~MVPP2_TXP_MTU_MAX;
  2374. val |= mtu;
  2375. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  2376. /* TXP token size and all TXQs token size must be larger that MTU */
  2377. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  2378. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  2379. if (size < mtu) {
  2380. size = mtu;
  2381. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  2382. val |= size;
  2383. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2384. }
  2385. for (txq = 0; txq < txq_number; txq++) {
  2386. val = mvpp2_read(port->priv,
  2387. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  2388. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  2389. if (size < mtu) {
  2390. size = mtu;
  2391. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  2392. val |= size;
  2393. mvpp2_write(port->priv,
  2394. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  2395. val);
  2396. }
  2397. }
  2398. }
  2399. /* Free Tx queue skbuffs */
  2400. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  2401. struct mvpp2_tx_queue *txq,
  2402. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  2403. {
  2404. int i;
  2405. for (i = 0; i < num; i++)
  2406. mvpp2_txq_inc_get(txq_pcpu);
  2407. }
  2408. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  2409. u32 cause)
  2410. {
  2411. int queue = fls(cause) - 1;
  2412. return port->rxqs[queue];
  2413. }
  2414. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  2415. u32 cause)
  2416. {
  2417. int queue = fls(cause) - 1;
  2418. return port->txqs[queue];
  2419. }
  2420. /* Rx/Tx queue initialization/cleanup methods */
  2421. /* Allocate and initialize descriptors for aggr TXQ */
  2422. static int mvpp2_aggr_txq_init(struct udevice *dev,
  2423. struct mvpp2_tx_queue *aggr_txq,
  2424. int desc_num, int cpu,
  2425. struct mvpp2 *priv)
  2426. {
  2427. /* Allocate memory for TX descriptors */
  2428. aggr_txq->descs = buffer_loc.aggr_tx_descs;
  2429. aggr_txq->descs_phys = (dma_addr_t)buffer_loc.aggr_tx_descs;
  2430. if (!aggr_txq->descs)
  2431. return -ENOMEM;
  2432. /* Make sure descriptor address is cache line size aligned */
  2433. BUG_ON(aggr_txq->descs !=
  2434. PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2435. aggr_txq->last_desc = aggr_txq->size - 1;
  2436. /* Aggr TXQ no reset WA */
  2437. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  2438. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  2439. /* Set Tx descriptors queue starting address */
  2440. /* indirect access */
  2441. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
  2442. aggr_txq->descs_phys);
  2443. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  2444. return 0;
  2445. }
  2446. /* Create a specified Rx queue */
  2447. static int mvpp2_rxq_init(struct mvpp2_port *port,
  2448. struct mvpp2_rx_queue *rxq)
  2449. {
  2450. rxq->size = port->rx_ring_size;
  2451. /* Allocate memory for RX descriptors */
  2452. rxq->descs = buffer_loc.rx_descs;
  2453. rxq->descs_phys = (dma_addr_t)buffer_loc.rx_descs;
  2454. if (!rxq->descs)
  2455. return -ENOMEM;
  2456. BUG_ON(rxq->descs !=
  2457. PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2458. rxq->last_desc = rxq->size - 1;
  2459. /* Zero occupied and non-occupied counters - direct access */
  2460. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2461. /* Set Rx descriptors queue starting address - indirect access */
  2462. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2463. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_phys);
  2464. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  2465. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  2466. /* Set Offset */
  2467. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  2468. /* Add number of descriptors ready for receiving packets */
  2469. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  2470. return 0;
  2471. }
  2472. /* Push packets received by the RXQ to BM pool */
  2473. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  2474. struct mvpp2_rx_queue *rxq)
  2475. {
  2476. int rx_received, i;
  2477. rx_received = mvpp2_rxq_received(port, rxq->id);
  2478. if (!rx_received)
  2479. return;
  2480. for (i = 0; i < rx_received; i++) {
  2481. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  2482. u32 bm = mvpp2_bm_cookie_build(rx_desc);
  2483. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  2484. rx_desc->buf_cookie);
  2485. }
  2486. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  2487. }
  2488. /* Cleanup Rx queue */
  2489. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  2490. struct mvpp2_rx_queue *rxq)
  2491. {
  2492. mvpp2_rxq_drop_pkts(port, rxq);
  2493. rxq->descs = NULL;
  2494. rxq->last_desc = 0;
  2495. rxq->next_desc_to_proc = 0;
  2496. rxq->descs_phys = 0;
  2497. /* Clear Rx descriptors queue starting address and size;
  2498. * free descriptor number
  2499. */
  2500. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2501. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2502. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  2503. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  2504. }
  2505. /* Create and initialize a Tx queue */
  2506. static int mvpp2_txq_init(struct mvpp2_port *port,
  2507. struct mvpp2_tx_queue *txq)
  2508. {
  2509. u32 val;
  2510. int cpu, desc, desc_per_txq, tx_port_num;
  2511. struct mvpp2_txq_pcpu *txq_pcpu;
  2512. txq->size = port->tx_ring_size;
  2513. /* Allocate memory for Tx descriptors */
  2514. txq->descs = buffer_loc.tx_descs;
  2515. txq->descs_phys = (dma_addr_t)buffer_loc.tx_descs;
  2516. if (!txq->descs)
  2517. return -ENOMEM;
  2518. /* Make sure descriptor address is cache line size aligned */
  2519. BUG_ON(txq->descs !=
  2520. PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2521. txq->last_desc = txq->size - 1;
  2522. /* Set Tx descriptors queue starting address - indirect access */
  2523. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2524. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_phys);
  2525. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  2526. MVPP2_TXQ_DESC_SIZE_MASK);
  2527. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  2528. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  2529. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  2530. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2531. val &= ~MVPP2_TXQ_PENDING_MASK;
  2532. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  2533. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  2534. * for each existing TXQ.
  2535. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  2536. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  2537. */
  2538. desc_per_txq = 16;
  2539. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  2540. (txq->log_id * desc_per_txq);
  2541. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  2542. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  2543. MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
  2544. /* WRR / EJP configuration - indirect access */
  2545. tx_port_num = mvpp2_egress_port(port);
  2546. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2547. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  2548. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  2549. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  2550. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  2551. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  2552. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  2553. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  2554. val);
  2555. for_each_present_cpu(cpu) {
  2556. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2557. txq_pcpu->size = txq->size;
  2558. }
  2559. return 0;
  2560. }
  2561. /* Free allocated TXQ resources */
  2562. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  2563. struct mvpp2_tx_queue *txq)
  2564. {
  2565. txq->descs = NULL;
  2566. txq->last_desc = 0;
  2567. txq->next_desc_to_proc = 0;
  2568. txq->descs_phys = 0;
  2569. /* Set minimum bandwidth for disabled TXQs */
  2570. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  2571. /* Set Tx descriptors queue starting address and size */
  2572. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2573. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  2574. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  2575. }
  2576. /* Cleanup Tx ports */
  2577. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  2578. {
  2579. struct mvpp2_txq_pcpu *txq_pcpu;
  2580. int delay, pending, cpu;
  2581. u32 val;
  2582. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2583. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  2584. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  2585. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2586. /* The napi queue has been stopped so wait for all packets
  2587. * to be transmitted.
  2588. */
  2589. delay = 0;
  2590. do {
  2591. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  2592. netdev_warn(port->dev,
  2593. "port %d: cleaning queue %d timed out\n",
  2594. port->id, txq->log_id);
  2595. break;
  2596. }
  2597. mdelay(1);
  2598. delay++;
  2599. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  2600. } while (pending);
  2601. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  2602. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2603. for_each_present_cpu(cpu) {
  2604. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2605. /* Release all packets */
  2606. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  2607. /* Reset queue */
  2608. txq_pcpu->count = 0;
  2609. txq_pcpu->txq_put_index = 0;
  2610. txq_pcpu->txq_get_index = 0;
  2611. }
  2612. }
  2613. /* Cleanup all Tx queues */
  2614. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  2615. {
  2616. struct mvpp2_tx_queue *txq;
  2617. int queue;
  2618. u32 val;
  2619. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  2620. /* Reset Tx ports and delete Tx queues */
  2621. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2622. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2623. for (queue = 0; queue < txq_number; queue++) {
  2624. txq = port->txqs[queue];
  2625. mvpp2_txq_clean(port, txq);
  2626. mvpp2_txq_deinit(port, txq);
  2627. }
  2628. mvpp2_txq_sent_counter_clear(port);
  2629. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2630. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2631. }
  2632. /* Cleanup all Rx queues */
  2633. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  2634. {
  2635. int queue;
  2636. for (queue = 0; queue < rxq_number; queue++)
  2637. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  2638. }
  2639. /* Init all Rx queues for port */
  2640. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  2641. {
  2642. int queue, err;
  2643. for (queue = 0; queue < rxq_number; queue++) {
  2644. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  2645. if (err)
  2646. goto err_cleanup;
  2647. }
  2648. return 0;
  2649. err_cleanup:
  2650. mvpp2_cleanup_rxqs(port);
  2651. return err;
  2652. }
  2653. /* Init all tx queues for port */
  2654. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  2655. {
  2656. struct mvpp2_tx_queue *txq;
  2657. int queue, err;
  2658. for (queue = 0; queue < txq_number; queue++) {
  2659. txq = port->txqs[queue];
  2660. err = mvpp2_txq_init(port, txq);
  2661. if (err)
  2662. goto err_cleanup;
  2663. }
  2664. mvpp2_txq_sent_counter_clear(port);
  2665. return 0;
  2666. err_cleanup:
  2667. mvpp2_cleanup_txqs(port);
  2668. return err;
  2669. }
  2670. /* Adjust link */
  2671. static void mvpp2_link_event(struct mvpp2_port *port)
  2672. {
  2673. struct phy_device *phydev = port->phy_dev;
  2674. int status_change = 0;
  2675. u32 val;
  2676. if (phydev->link) {
  2677. if ((port->speed != phydev->speed) ||
  2678. (port->duplex != phydev->duplex)) {
  2679. u32 val;
  2680. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2681. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  2682. MVPP2_GMAC_CONFIG_GMII_SPEED |
  2683. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  2684. MVPP2_GMAC_AN_SPEED_EN |
  2685. MVPP2_GMAC_AN_DUPLEX_EN);
  2686. if (phydev->duplex)
  2687. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  2688. if (phydev->speed == SPEED_1000)
  2689. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  2690. else if (phydev->speed == SPEED_100)
  2691. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  2692. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2693. port->duplex = phydev->duplex;
  2694. port->speed = phydev->speed;
  2695. }
  2696. }
  2697. if (phydev->link != port->link) {
  2698. if (!phydev->link) {
  2699. port->duplex = -1;
  2700. port->speed = 0;
  2701. }
  2702. port->link = phydev->link;
  2703. status_change = 1;
  2704. }
  2705. if (status_change) {
  2706. if (phydev->link) {
  2707. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2708. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  2709. MVPP2_GMAC_FORCE_LINK_DOWN);
  2710. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2711. mvpp2_egress_enable(port);
  2712. mvpp2_ingress_enable(port);
  2713. } else {
  2714. mvpp2_ingress_disable(port);
  2715. mvpp2_egress_disable(port);
  2716. }
  2717. }
  2718. }
  2719. /* Main RX/TX processing routines */
  2720. /* Display more error info */
  2721. static void mvpp2_rx_error(struct mvpp2_port *port,
  2722. struct mvpp2_rx_desc *rx_desc)
  2723. {
  2724. u32 status = rx_desc->status;
  2725. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  2726. case MVPP2_RXD_ERR_CRC:
  2727. netdev_err(port->dev, "bad rx status %08x (crc error), size=%d\n",
  2728. status, rx_desc->data_size);
  2729. break;
  2730. case MVPP2_RXD_ERR_OVERRUN:
  2731. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%d\n",
  2732. status, rx_desc->data_size);
  2733. break;
  2734. case MVPP2_RXD_ERR_RESOURCE:
  2735. netdev_err(port->dev, "bad rx status %08x (resource error), size=%d\n",
  2736. status, rx_desc->data_size);
  2737. break;
  2738. }
  2739. }
  2740. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  2741. static int mvpp2_rx_refill(struct mvpp2_port *port,
  2742. struct mvpp2_bm_pool *bm_pool,
  2743. u32 bm, u32 phys_addr)
  2744. {
  2745. mvpp2_pool_refill(port, bm, phys_addr, (unsigned long)phys_addr);
  2746. return 0;
  2747. }
  2748. /* Set hw internals when starting port */
  2749. static void mvpp2_start_dev(struct mvpp2_port *port)
  2750. {
  2751. mvpp2_gmac_max_rx_size_set(port);
  2752. mvpp2_txp_max_tx_size_set(port);
  2753. mvpp2_port_enable(port);
  2754. }
  2755. /* Set hw internals when stopping port */
  2756. static void mvpp2_stop_dev(struct mvpp2_port *port)
  2757. {
  2758. /* Stop new packets from arriving to RXQs */
  2759. mvpp2_ingress_disable(port);
  2760. mvpp2_egress_disable(port);
  2761. mvpp2_port_disable(port);
  2762. }
  2763. static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
  2764. {
  2765. struct phy_device *phy_dev;
  2766. if (!port->init || port->link == 0) {
  2767. phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
  2768. port->phy_interface);
  2769. port->phy_dev = phy_dev;
  2770. if (!phy_dev) {
  2771. netdev_err(port->dev, "cannot connect to phy\n");
  2772. return -ENODEV;
  2773. }
  2774. phy_dev->supported &= PHY_GBIT_FEATURES;
  2775. phy_dev->advertising = phy_dev->supported;
  2776. port->phy_dev = phy_dev;
  2777. port->link = 0;
  2778. port->duplex = 0;
  2779. port->speed = 0;
  2780. phy_config(phy_dev);
  2781. phy_startup(phy_dev);
  2782. if (!phy_dev->link) {
  2783. printf("%s: No link\n", phy_dev->dev->name);
  2784. return -1;
  2785. }
  2786. port->init = 1;
  2787. } else {
  2788. mvpp2_egress_enable(port);
  2789. mvpp2_ingress_enable(port);
  2790. }
  2791. return 0;
  2792. }
  2793. static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
  2794. {
  2795. unsigned char mac_bcast[ETH_ALEN] = {
  2796. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2797. int err;
  2798. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  2799. if (err) {
  2800. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  2801. return err;
  2802. }
  2803. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  2804. port->dev_addr, true);
  2805. if (err) {
  2806. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  2807. return err;
  2808. }
  2809. err = mvpp2_prs_def_flow(port);
  2810. if (err) {
  2811. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  2812. return err;
  2813. }
  2814. /* Allocate the Rx/Tx queues */
  2815. err = mvpp2_setup_rxqs(port);
  2816. if (err) {
  2817. netdev_err(port->dev, "cannot allocate Rx queues\n");
  2818. return err;
  2819. }
  2820. err = mvpp2_setup_txqs(port);
  2821. if (err) {
  2822. netdev_err(port->dev, "cannot allocate Tx queues\n");
  2823. return err;
  2824. }
  2825. err = mvpp2_phy_connect(dev, port);
  2826. if (err < 0)
  2827. return err;
  2828. mvpp2_link_event(port);
  2829. mvpp2_start_dev(port);
  2830. return 0;
  2831. }
  2832. /* No Device ops here in U-Boot */
  2833. /* Driver initialization */
  2834. static void mvpp2_port_power_up(struct mvpp2_port *port)
  2835. {
  2836. mvpp2_port_mii_set(port);
  2837. mvpp2_port_periodic_xon_disable(port);
  2838. mvpp2_port_fc_adv_enable(port);
  2839. mvpp2_port_reset(port);
  2840. }
  2841. /* Initialize port HW */
  2842. static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
  2843. {
  2844. struct mvpp2 *priv = port->priv;
  2845. struct mvpp2_txq_pcpu *txq_pcpu;
  2846. int queue, cpu, err;
  2847. if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
  2848. return -EINVAL;
  2849. /* Disable port */
  2850. mvpp2_egress_disable(port);
  2851. mvpp2_port_disable(port);
  2852. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  2853. GFP_KERNEL);
  2854. if (!port->txqs)
  2855. return -ENOMEM;
  2856. /* Associate physical Tx queues to this port and initialize.
  2857. * The mapping is predefined.
  2858. */
  2859. for (queue = 0; queue < txq_number; queue++) {
  2860. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  2861. struct mvpp2_tx_queue *txq;
  2862. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  2863. if (!txq)
  2864. return -ENOMEM;
  2865. txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
  2866. GFP_KERNEL);
  2867. if (!txq->pcpu)
  2868. return -ENOMEM;
  2869. txq->id = queue_phy_id;
  2870. txq->log_id = queue;
  2871. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  2872. for_each_present_cpu(cpu) {
  2873. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2874. txq_pcpu->cpu = cpu;
  2875. }
  2876. port->txqs[queue] = txq;
  2877. }
  2878. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  2879. GFP_KERNEL);
  2880. if (!port->rxqs)
  2881. return -ENOMEM;
  2882. /* Allocate and initialize Rx queue for this port */
  2883. for (queue = 0; queue < rxq_number; queue++) {
  2884. struct mvpp2_rx_queue *rxq;
  2885. /* Map physical Rx queue to port's logical Rx queue */
  2886. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  2887. if (!rxq)
  2888. return -ENOMEM;
  2889. /* Map this Rx queue to a physical queue */
  2890. rxq->id = port->first_rxq + queue;
  2891. rxq->port = port->id;
  2892. rxq->logic_rxq = queue;
  2893. port->rxqs[queue] = rxq;
  2894. }
  2895. /* Configure Rx queue group interrupt for this port */
  2896. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
  2897. /* Create Rx descriptor rings */
  2898. for (queue = 0; queue < rxq_number; queue++) {
  2899. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  2900. rxq->size = port->rx_ring_size;
  2901. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  2902. rxq->time_coal = MVPP2_RX_COAL_USEC;
  2903. }
  2904. mvpp2_ingress_disable(port);
  2905. /* Port default configuration */
  2906. mvpp2_defaults_set(port);
  2907. /* Port's classifier configuration */
  2908. mvpp2_cls_oversize_rxq_set(port);
  2909. mvpp2_cls_port_config(port);
  2910. /* Provide an initial Rx packet size */
  2911. port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
  2912. /* Initialize pools for swf */
  2913. err = mvpp2_swf_bm_pool_init(port);
  2914. if (err)
  2915. return err;
  2916. return 0;
  2917. }
  2918. /* Ports initialization */
  2919. static int mvpp2_port_probe(struct udevice *dev,
  2920. struct mvpp2_port *port,
  2921. int port_node,
  2922. struct mvpp2 *priv,
  2923. int *next_first_rxq)
  2924. {
  2925. int phy_node;
  2926. u32 id;
  2927. u32 phyaddr;
  2928. const char *phy_mode_str;
  2929. int phy_mode = -1;
  2930. int priv_common_regs_num = 2;
  2931. int err;
  2932. phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
  2933. if (phy_node < 0) {
  2934. dev_err(&pdev->dev, "missing phy\n");
  2935. return -ENODEV;
  2936. }
  2937. phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
  2938. if (phy_mode_str)
  2939. phy_mode = phy_get_interface_by_name(phy_mode_str);
  2940. if (phy_mode == -1) {
  2941. dev_err(&pdev->dev, "incorrect phy mode\n");
  2942. return -EINVAL;
  2943. }
  2944. id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
  2945. if (id == -1) {
  2946. dev_err(&pdev->dev, "missing port-id value\n");
  2947. return -EINVAL;
  2948. }
  2949. phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
  2950. port->priv = priv;
  2951. port->id = id;
  2952. port->first_rxq = *next_first_rxq;
  2953. port->phy_node = phy_node;
  2954. port->phy_interface = phy_mode;
  2955. port->phyaddr = phyaddr;
  2956. port->base = (void __iomem *)dev_get_addr_index(dev->parent,
  2957. priv_common_regs_num
  2958. + id);
  2959. if (IS_ERR(port->base))
  2960. return PTR_ERR(port->base);
  2961. port->tx_ring_size = MVPP2_MAX_TXD;
  2962. port->rx_ring_size = MVPP2_MAX_RXD;
  2963. err = mvpp2_port_init(dev, port);
  2964. if (err < 0) {
  2965. dev_err(&pdev->dev, "failed to init port %d\n", id);
  2966. return err;
  2967. }
  2968. mvpp2_port_power_up(port);
  2969. /* Increment the first Rx queue number to be used by the next port */
  2970. *next_first_rxq += CONFIG_MV_ETH_RXQ;
  2971. priv->port_list[id] = port;
  2972. return 0;
  2973. }
  2974. /* Initialize decoding windows */
  2975. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  2976. struct mvpp2 *priv)
  2977. {
  2978. u32 win_enable;
  2979. int i;
  2980. for (i = 0; i < 6; i++) {
  2981. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  2982. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  2983. if (i < 4)
  2984. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  2985. }
  2986. win_enable = 0;
  2987. for (i = 0; i < dram->num_cs; i++) {
  2988. const struct mbus_dram_window *cs = dram->cs + i;
  2989. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  2990. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  2991. dram->mbus_dram_target_id);
  2992. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  2993. (cs->size - 1) & 0xffff0000);
  2994. win_enable |= (1 << i);
  2995. }
  2996. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  2997. }
  2998. /* Initialize Rx FIFO's */
  2999. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  3000. {
  3001. int port;
  3002. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  3003. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3004. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  3005. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3006. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  3007. }
  3008. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  3009. MVPP2_RX_FIFO_PORT_MIN_PKT);
  3010. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  3011. }
  3012. /* Initialize network controller common part HW */
  3013. static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
  3014. {
  3015. const struct mbus_dram_target_info *dram_target_info;
  3016. int err, i;
  3017. u32 val;
  3018. /* Checks for hardware constraints (U-Boot uses only one rxq) */
  3019. if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
  3020. dev_err(&pdev->dev, "invalid queue size parameter\n");
  3021. return -EINVAL;
  3022. }
  3023. /* MBUS windows configuration */
  3024. dram_target_info = mvebu_mbus_dram_info();
  3025. if (dram_target_info)
  3026. mvpp2_conf_mbus_windows(dram_target_info, priv);
  3027. /* Disable HW PHY polling */
  3028. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3029. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  3030. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3031. /* Allocate and initialize aggregated TXQs */
  3032. priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
  3033. sizeof(struct mvpp2_tx_queue),
  3034. GFP_KERNEL);
  3035. if (!priv->aggr_txqs)
  3036. return -ENOMEM;
  3037. for_each_present_cpu(i) {
  3038. priv->aggr_txqs[i].id = i;
  3039. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  3040. err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
  3041. MVPP2_AGGR_TXQ_SIZE, i, priv);
  3042. if (err < 0)
  3043. return err;
  3044. }
  3045. /* Rx Fifo Init */
  3046. mvpp2_rx_fifo_init(priv);
  3047. /* Reset Rx queue group interrupt configuration */
  3048. for (i = 0; i < MVPP2_MAX_PORTS; i++)
  3049. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
  3050. CONFIG_MV_ETH_RXQ);
  3051. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  3052. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  3053. /* Allow cache snoop when transmiting packets */
  3054. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  3055. /* Buffer Manager initialization */
  3056. err = mvpp2_bm_init(dev, priv);
  3057. if (err < 0)
  3058. return err;
  3059. /* Parser default initialization */
  3060. err = mvpp2_prs_default_init(dev, priv);
  3061. if (err < 0)
  3062. return err;
  3063. /* Classifier default initialization */
  3064. mvpp2_cls_init(priv);
  3065. return 0;
  3066. }
  3067. /* SMI / MDIO functions */
  3068. static int smi_wait_ready(struct mvpp2 *priv)
  3069. {
  3070. u32 timeout = MVPP2_SMI_TIMEOUT;
  3071. u32 smi_reg;
  3072. /* wait till the SMI is not busy */
  3073. do {
  3074. /* read smi register */
  3075. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3076. if (timeout-- == 0) {
  3077. printf("Error: SMI busy timeout\n");
  3078. return -EFAULT;
  3079. }
  3080. } while (smi_reg & MVPP2_SMI_BUSY);
  3081. return 0;
  3082. }
  3083. /*
  3084. * mpp2_mdio_read - miiphy_read callback function.
  3085. *
  3086. * Returns 16bit phy register value, or 0xffff on error
  3087. */
  3088. static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  3089. {
  3090. struct mvpp2 *priv = bus->priv;
  3091. u32 smi_reg;
  3092. u32 timeout;
  3093. /* check parameters */
  3094. if (addr > MVPP2_PHY_ADDR_MASK) {
  3095. printf("Error: Invalid PHY address %d\n", addr);
  3096. return -EFAULT;
  3097. }
  3098. if (reg > MVPP2_PHY_REG_MASK) {
  3099. printf("Err: Invalid register offset %d\n", reg);
  3100. return -EFAULT;
  3101. }
  3102. /* wait till the SMI is not busy */
  3103. if (smi_wait_ready(priv) < 0)
  3104. return -EFAULT;
  3105. /* fill the phy address and regiser offset and read opcode */
  3106. smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3107. | (reg << MVPP2_SMI_REG_ADDR_OFFS)
  3108. | MVPP2_SMI_OPCODE_READ;
  3109. /* write the smi register */
  3110. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3111. /* wait till read value is ready */
  3112. timeout = MVPP2_SMI_TIMEOUT;
  3113. do {
  3114. /* read smi register */
  3115. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3116. if (timeout-- == 0) {
  3117. printf("Err: SMI read ready timeout\n");
  3118. return -EFAULT;
  3119. }
  3120. } while (!(smi_reg & MVPP2_SMI_READ_VALID));
  3121. /* Wait for the data to update in the SMI register */
  3122. for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
  3123. ;
  3124. return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
  3125. }
  3126. /*
  3127. * mpp2_mdio_write - miiphy_write callback function.
  3128. *
  3129. * Returns 0 if write succeed, -EINVAL on bad parameters
  3130. * -ETIME on timeout
  3131. */
  3132. static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  3133. u16 value)
  3134. {
  3135. struct mvpp2 *priv = bus->priv;
  3136. u32 smi_reg;
  3137. /* check parameters */
  3138. if (addr > MVPP2_PHY_ADDR_MASK) {
  3139. printf("Error: Invalid PHY address %d\n", addr);
  3140. return -EFAULT;
  3141. }
  3142. if (reg > MVPP2_PHY_REG_MASK) {
  3143. printf("Err: Invalid register offset %d\n", reg);
  3144. return -EFAULT;
  3145. }
  3146. /* wait till the SMI is not busy */
  3147. if (smi_wait_ready(priv) < 0)
  3148. return -EFAULT;
  3149. /* fill the phy addr and reg offset and write opcode and data */
  3150. smi_reg = value << MVPP2_SMI_DATA_OFFS;
  3151. smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3152. | (reg << MVPP2_SMI_REG_ADDR_OFFS);
  3153. smi_reg &= ~MVPP2_SMI_OPCODE_READ;
  3154. /* write the smi register */
  3155. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3156. return 0;
  3157. }
  3158. static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
  3159. {
  3160. struct mvpp2_port *port = dev_get_priv(dev);
  3161. struct mvpp2_rx_desc *rx_desc;
  3162. struct mvpp2_bm_pool *bm_pool;
  3163. dma_addr_t phys_addr;
  3164. u32 bm, rx_status;
  3165. int pool, rx_bytes, err;
  3166. int rx_received;
  3167. struct mvpp2_rx_queue *rxq;
  3168. u32 cause_rx_tx, cause_rx, cause_misc;
  3169. u8 *data;
  3170. cause_rx_tx = mvpp2_read(port->priv,
  3171. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  3172. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  3173. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  3174. if (!cause_rx_tx && !cause_misc)
  3175. return 0;
  3176. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3177. /* Process RX packets */
  3178. cause_rx |= port->pending_cause_rx;
  3179. rxq = mvpp2_get_rx_queue(port, cause_rx);
  3180. /* Get number of received packets and clamp the to-do */
  3181. rx_received = mvpp2_rxq_received(port, rxq->id);
  3182. /* Return if no packets are received */
  3183. if (!rx_received)
  3184. return 0;
  3185. rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3186. rx_status = rx_desc->status;
  3187. rx_bytes = rx_desc->data_size - MVPP2_MH_SIZE;
  3188. phys_addr = rx_desc->buf_phys_addr;
  3189. bm = mvpp2_bm_cookie_build(rx_desc);
  3190. pool = mvpp2_bm_cookie_pool_get(bm);
  3191. bm_pool = &port->priv->bm_pools[pool];
  3192. /* Check if buffer header is used */
  3193. if (rx_status & MVPP2_RXD_BUF_HDR)
  3194. return 0;
  3195. /* In case of an error, release the requested buffer pointer
  3196. * to the Buffer Manager. This request process is controlled
  3197. * by the hardware, and the information about the buffer is
  3198. * comprised by the RX descriptor.
  3199. */
  3200. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  3201. mvpp2_rx_error(port, rx_desc);
  3202. /* Return the buffer to the pool */
  3203. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  3204. rx_desc->buf_cookie);
  3205. return 0;
  3206. }
  3207. err = mvpp2_rx_refill(port, bm_pool, bm, phys_addr);
  3208. if (err) {
  3209. netdev_err(port->dev, "failed to refill BM pools\n");
  3210. return 0;
  3211. }
  3212. /* Update Rx queue management counters */
  3213. mb();
  3214. mvpp2_rxq_status_update(port, rxq->id, 1, 1);
  3215. /* give packet to stack - skip on first n bytes */
  3216. data = (u8 *)phys_addr + 2 + 32;
  3217. if (rx_bytes <= 0)
  3218. return 0;
  3219. /*
  3220. * No cache invalidation needed here, since the rx_buffer's are
  3221. * located in a uncached memory region
  3222. */
  3223. *packetp = data;
  3224. return rx_bytes;
  3225. }
  3226. /* Drain Txq */
  3227. static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  3228. int enable)
  3229. {
  3230. u32 val;
  3231. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3232. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3233. if (enable)
  3234. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3235. else
  3236. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3237. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3238. }
  3239. static int mvpp2_send(struct udevice *dev, void *packet, int length)
  3240. {
  3241. struct mvpp2_port *port = dev_get_priv(dev);
  3242. struct mvpp2_tx_queue *txq, *aggr_txq;
  3243. struct mvpp2_tx_desc *tx_desc;
  3244. int tx_done;
  3245. int timeout;
  3246. txq = port->txqs[0];
  3247. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  3248. /* Get a descriptor for the first part of the packet */
  3249. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  3250. tx_desc->phys_txq = txq->id;
  3251. tx_desc->data_size = length;
  3252. tx_desc->packet_offset = (u32)packet & MVPP2_TX_DESC_ALIGN;
  3253. tx_desc->buf_phys_addr = (u32)packet & ~MVPP2_TX_DESC_ALIGN;
  3254. /* First and Last descriptor */
  3255. tx_desc->command = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
  3256. | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
  3257. /* Flush tx data */
  3258. flush_dcache_range((unsigned long)packet,
  3259. (unsigned long)packet + ALIGN(length, PKTALIGN));
  3260. /* Enable transmit */
  3261. mb();
  3262. mvpp2_aggr_txq_pend_desc_add(port, 1);
  3263. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3264. timeout = 0;
  3265. do {
  3266. if (timeout++ > 10000) {
  3267. printf("timeout: packet not sent from aggregated to phys TXQ\n");
  3268. return 0;
  3269. }
  3270. tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
  3271. } while (tx_done);
  3272. /* Enable TXQ drain */
  3273. mvpp2_txq_drain(port, txq, 1);
  3274. timeout = 0;
  3275. do {
  3276. if (timeout++ > 10000) {
  3277. printf("timeout: packet not sent\n");
  3278. return 0;
  3279. }
  3280. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  3281. } while (!tx_done);
  3282. /* Disable TXQ drain */
  3283. mvpp2_txq_drain(port, txq, 0);
  3284. return 0;
  3285. }
  3286. static int mvpp2_start(struct udevice *dev)
  3287. {
  3288. struct eth_pdata *pdata = dev_get_platdata(dev);
  3289. struct mvpp2_port *port = dev_get_priv(dev);
  3290. /* Load current MAC address */
  3291. memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
  3292. /* Reconfigure parser accept the original MAC address */
  3293. mvpp2_prs_update_mac_da(port, port->dev_addr);
  3294. mvpp2_port_power_up(port);
  3295. mvpp2_open(dev, port);
  3296. return 0;
  3297. }
  3298. static void mvpp2_stop(struct udevice *dev)
  3299. {
  3300. struct mvpp2_port *port = dev_get_priv(dev);
  3301. mvpp2_stop_dev(port);
  3302. mvpp2_cleanup_rxqs(port);
  3303. mvpp2_cleanup_txqs(port);
  3304. }
  3305. static int mvpp2_probe(struct udevice *dev)
  3306. {
  3307. struct mvpp2_port *port = dev_get_priv(dev);
  3308. struct mvpp2 *priv = dev_get_priv(dev->parent);
  3309. int err;
  3310. /* Initialize network controller */
  3311. err = mvpp2_init(dev, priv);
  3312. if (err < 0) {
  3313. dev_err(&pdev->dev, "failed to initialize controller\n");
  3314. return err;
  3315. }
  3316. return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
  3317. &buffer_loc.first_rxq);
  3318. }
  3319. static const struct eth_ops mvpp2_ops = {
  3320. .start = mvpp2_start,
  3321. .send = mvpp2_send,
  3322. .recv = mvpp2_recv,
  3323. .stop = mvpp2_stop,
  3324. };
  3325. static struct driver mvpp2_driver = {
  3326. .name = "mvpp2",
  3327. .id = UCLASS_ETH,
  3328. .probe = mvpp2_probe,
  3329. .ops = &mvpp2_ops,
  3330. .priv_auto_alloc_size = sizeof(struct mvpp2_port),
  3331. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  3332. };
  3333. /*
  3334. * Use a MISC device to bind the n instances (child nodes) of the
  3335. * network base controller in UCLASS_ETH.
  3336. */
  3337. static int mvpp2_base_probe(struct udevice *dev)
  3338. {
  3339. struct mvpp2 *priv = dev_get_priv(dev);
  3340. struct mii_dev *bus;
  3341. void *bd_space;
  3342. u32 size = 0;
  3343. int i;
  3344. /*
  3345. * U-Boot special buffer handling:
  3346. *
  3347. * Allocate buffer area for descs and rx_buffers. This is only
  3348. * done once for all interfaces. As only one interface can
  3349. * be active. Make this area DMA-safe by disabling the D-cache
  3350. */
  3351. /* Align buffer area for descs and rx_buffers to 1MiB */
  3352. bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
  3353. mmu_set_region_dcache_behaviour((u32)bd_space, BD_SPACE, DCACHE_OFF);
  3354. buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
  3355. size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
  3356. buffer_loc.tx_descs = (struct mvpp2_tx_desc *)((u32)bd_space + size);
  3357. size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
  3358. buffer_loc.rx_descs = (struct mvpp2_rx_desc *)((u32)bd_space + size);
  3359. size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
  3360. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3361. buffer_loc.bm_pool[i] = (u32 *)((u32)bd_space + size);
  3362. size += MVPP2_BM_POOL_SIZE_MAX * sizeof(u32);
  3363. }
  3364. for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
  3365. buffer_loc.rx_buffer[i] = (u32 *)((u32)bd_space + size);
  3366. size += RX_BUFFER_SIZE;
  3367. }
  3368. /* Save base addresses for later use */
  3369. priv->base = (void *)dev_get_addr_index(dev, 0);
  3370. if (IS_ERR(priv->base))
  3371. return PTR_ERR(priv->base);
  3372. priv->lms_base = (void *)dev_get_addr_index(dev, 1);
  3373. if (IS_ERR(priv->lms_base))
  3374. return PTR_ERR(priv->lms_base);
  3375. /* Finally create and register the MDIO bus driver */
  3376. bus = mdio_alloc();
  3377. if (!bus) {
  3378. printf("Failed to allocate MDIO bus\n");
  3379. return -ENOMEM;
  3380. }
  3381. bus->read = mpp2_mdio_read;
  3382. bus->write = mpp2_mdio_write;
  3383. snprintf(bus->name, sizeof(bus->name), dev->name);
  3384. bus->priv = (void *)priv;
  3385. priv->bus = bus;
  3386. return mdio_register(bus);
  3387. }
  3388. static int mvpp2_base_bind(struct udevice *parent)
  3389. {
  3390. const void *blob = gd->fdt_blob;
  3391. int node = dev_of_offset(parent);
  3392. struct uclass_driver *drv;
  3393. struct udevice *dev;
  3394. struct eth_pdata *plat;
  3395. char *name;
  3396. int subnode;
  3397. u32 id;
  3398. /* Lookup eth driver */
  3399. drv = lists_uclass_lookup(UCLASS_ETH);
  3400. if (!drv) {
  3401. puts("Cannot find eth driver\n");
  3402. return -ENOENT;
  3403. }
  3404. fdt_for_each_subnode(subnode, blob, node) {
  3405. /* Skip disabled ports */
  3406. if (!fdtdec_get_is_enabled(blob, subnode))
  3407. continue;
  3408. plat = calloc(1, sizeof(*plat));
  3409. if (!plat)
  3410. return -ENOMEM;
  3411. id = fdtdec_get_int(blob, subnode, "port-id", -1);
  3412. name = calloc(1, 16);
  3413. sprintf(name, "mvpp2-%d", id);
  3414. /* Create child device UCLASS_ETH and bind it */
  3415. device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
  3416. dev_set_of_offset(dev, subnode);
  3417. }
  3418. return 0;
  3419. }
  3420. static const struct udevice_id mvpp2_ids[] = {
  3421. { .compatible = "marvell,armada-375-pp2" },
  3422. { }
  3423. };
  3424. U_BOOT_DRIVER(mvpp2_base) = {
  3425. .name = "mvpp2_base",
  3426. .id = UCLASS_MISC,
  3427. .of_match = mvpp2_ids,
  3428. .bind = mvpp2_base_bind,
  3429. .probe = mvpp2_base_probe,
  3430. .priv_auto_alloc_size = sizeof(struct mvpp2),
  3431. };