mvneta.c 131 KB

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