mvpp2.c 126 KB

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