macb_main.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cadence MACB/GEM Ethernet Controller driver
  4. *
  5. * Copyright (C) 2004-2006 Atmel Corporation
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/crc32.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/circ_buf.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/phylink.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_mdio.h>
  31. #include <linux/of_net.h>
  32. #include <linux/ip.h>
  33. #include <linux/udp.h>
  34. #include <linux/tcp.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/phy/phy.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/ptp_classify.h>
  39. #include <linux/reset.h>
  40. #include <linux/firmware/xlnx-zynqmp.h>
  41. #include "macb.h"
  42. /* This structure is only used for MACB on SiFive FU540 devices */
  43. struct sifive_fu540_macb_mgmt {
  44. void __iomem *reg;
  45. unsigned long rate;
  46. struct clk_hw hw;
  47. };
  48. #define MACB_RX_BUFFER_SIZE 128
  49. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  50. #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
  51. #define MIN_RX_RING_SIZE 64
  52. #define MAX_RX_RING_SIZE 8192
  53. #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  54. * (bp)->rx_ring_size)
  55. #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
  56. #define MIN_TX_RING_SIZE 64
  57. #define MAX_TX_RING_SIZE 4096
  58. #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  59. * (bp)->tx_ring_size)
  60. /* level of occupied TX descriptors under which we wake up TX process */
  61. #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
  62. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
  63. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  64. | MACB_BIT(ISR_RLE) \
  65. | MACB_BIT(TXERR))
  66. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
  67. | MACB_BIT(TXUBR))
  68. /* Max length of transmit frame must be a multiple of 8 bytes */
  69. #define MACB_TX_LEN_ALIGN 8
  70. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  71. /* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
  72. * false amba_error in TX path from the DMA assuming there is not enough
  73. * space in the SRAM (16KB) even when there is.
  74. */
  75. #define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
  76. #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
  77. #define MACB_NETIF_LSO NETIF_F_TSO
  78. #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  79. #define MACB_WOL_ENABLED (0x1 << 1)
  80. #define HS_SPEED_10000M 4
  81. #define MACB_SERDES_RATE_10G 1
  82. /* Graceful stop timeouts in us. We should allow up to
  83. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  84. */
  85. #define MACB_HALT_TIMEOUT 1230
  86. #define MACB_PM_TIMEOUT 100 /* ms */
  87. #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
  88. /* DMA buffer descriptor might be different size
  89. * depends on hardware configuration:
  90. *
  91. * 1. dma address width 32 bits:
  92. * word 1: 32 bit address of Data Buffer
  93. * word 2: control
  94. *
  95. * 2. dma address width 64 bits:
  96. * word 1: 32 bit address of Data Buffer
  97. * word 2: control
  98. * word 3: upper 32 bit address of Data Buffer
  99. * word 4: unused
  100. *
  101. * 3. dma address width 32 bits with hardware timestamping:
  102. * word 1: 32 bit address of Data Buffer
  103. * word 2: control
  104. * word 3: timestamp word 1
  105. * word 4: timestamp word 2
  106. *
  107. * 4. dma address width 64 bits with hardware timestamping:
  108. * word 1: 32 bit address of Data Buffer
  109. * word 2: control
  110. * word 3: upper 32 bit address of Data Buffer
  111. * word 4: unused
  112. * word 5: timestamp word 1
  113. * word 6: timestamp word 2
  114. */
  115. static unsigned int macb_dma_desc_get_size(struct macb *bp)
  116. {
  117. #ifdef MACB_EXT_DESC
  118. unsigned int desc_size;
  119. switch (bp->hw_dma_cap) {
  120. case HW_DMA_CAP_64B:
  121. desc_size = sizeof(struct macb_dma_desc)
  122. + sizeof(struct macb_dma_desc_64);
  123. break;
  124. case HW_DMA_CAP_PTP:
  125. desc_size = sizeof(struct macb_dma_desc)
  126. + sizeof(struct macb_dma_desc_ptp);
  127. break;
  128. case HW_DMA_CAP_64B_PTP:
  129. desc_size = sizeof(struct macb_dma_desc)
  130. + sizeof(struct macb_dma_desc_64)
  131. + sizeof(struct macb_dma_desc_ptp);
  132. break;
  133. default:
  134. desc_size = sizeof(struct macb_dma_desc);
  135. }
  136. return desc_size;
  137. #endif
  138. return sizeof(struct macb_dma_desc);
  139. }
  140. static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
  141. {
  142. #ifdef MACB_EXT_DESC
  143. switch (bp->hw_dma_cap) {
  144. case HW_DMA_CAP_64B:
  145. case HW_DMA_CAP_PTP:
  146. desc_idx <<= 1;
  147. break;
  148. case HW_DMA_CAP_64B_PTP:
  149. desc_idx *= 3;
  150. break;
  151. default:
  152. break;
  153. }
  154. #endif
  155. return desc_idx;
  156. }
  157. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  158. static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
  159. {
  160. return (struct macb_dma_desc_64 *)((void *)desc
  161. + sizeof(struct macb_dma_desc));
  162. }
  163. #endif
  164. /* Ring buffer accessors */
  165. static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
  166. {
  167. return index & (bp->tx_ring_size - 1);
  168. }
  169. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  170. unsigned int index)
  171. {
  172. index = macb_tx_ring_wrap(queue->bp, index);
  173. index = macb_adj_dma_desc_idx(queue->bp, index);
  174. return &queue->tx_ring[index];
  175. }
  176. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  177. unsigned int index)
  178. {
  179. return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
  180. }
  181. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  182. {
  183. dma_addr_t offset;
  184. offset = macb_tx_ring_wrap(queue->bp, index) *
  185. macb_dma_desc_get_size(queue->bp);
  186. return queue->tx_ring_dma + offset;
  187. }
  188. static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
  189. {
  190. return index & (bp->rx_ring_size - 1);
  191. }
  192. static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
  193. {
  194. index = macb_rx_ring_wrap(queue->bp, index);
  195. index = macb_adj_dma_desc_idx(queue->bp, index);
  196. return &queue->rx_ring[index];
  197. }
  198. static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
  199. {
  200. return queue->rx_buffers + queue->bp->rx_buffer_size *
  201. macb_rx_ring_wrap(queue->bp, index);
  202. }
  203. /* I/O accessors */
  204. static u32 hw_readl_native(struct macb *bp, int offset)
  205. {
  206. return __raw_readl(bp->regs + offset);
  207. }
  208. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  209. {
  210. __raw_writel(value, bp->regs + offset);
  211. }
  212. static u32 hw_readl(struct macb *bp, int offset)
  213. {
  214. return readl_relaxed(bp->regs + offset);
  215. }
  216. static void hw_writel(struct macb *bp, int offset, u32 value)
  217. {
  218. writel_relaxed(value, bp->regs + offset);
  219. }
  220. /* Find the CPU endianness by using the loopback bit of NCR register. When the
  221. * CPU is in big endian we need to program swapped mode for management
  222. * descriptor access.
  223. */
  224. static bool hw_is_native_io(void __iomem *addr)
  225. {
  226. u32 value = MACB_BIT(LLB);
  227. __raw_writel(value, addr + MACB_NCR);
  228. value = __raw_readl(addr + MACB_NCR);
  229. /* Write 0 back to disable everything */
  230. __raw_writel(0, addr + MACB_NCR);
  231. return value == MACB_BIT(LLB);
  232. }
  233. static bool hw_is_gem(void __iomem *addr, bool native_io)
  234. {
  235. u32 id;
  236. if (native_io)
  237. id = __raw_readl(addr + MACB_MID);
  238. else
  239. id = readl_relaxed(addr + MACB_MID);
  240. return MACB_BFEXT(IDNUM, id) >= 0x2;
  241. }
  242. static void macb_set_hwaddr(struct macb *bp)
  243. {
  244. u32 bottom;
  245. u16 top;
  246. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  247. macb_or_gem_writel(bp, SA1B, bottom);
  248. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  249. macb_or_gem_writel(bp, SA1T, top);
  250. if (gem_has_ptp(bp)) {
  251. gem_writel(bp, RXPTPUNI, bottom);
  252. gem_writel(bp, TXPTPUNI, bottom);
  253. }
  254. /* Clear unused address register sets */
  255. macb_or_gem_writel(bp, SA2B, 0);
  256. macb_or_gem_writel(bp, SA2T, 0);
  257. macb_or_gem_writel(bp, SA3B, 0);
  258. macb_or_gem_writel(bp, SA3T, 0);
  259. macb_or_gem_writel(bp, SA4B, 0);
  260. macb_or_gem_writel(bp, SA4T, 0);
  261. }
  262. static void macb_get_hwaddr(struct macb *bp)
  263. {
  264. u32 bottom;
  265. u16 top;
  266. u8 addr[6];
  267. int i;
  268. /* Check all 4 address register for valid address */
  269. for (i = 0; i < 4; i++) {
  270. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  271. top = macb_or_gem_readl(bp, SA1T + i * 8);
  272. addr[0] = bottom & 0xff;
  273. addr[1] = (bottom >> 8) & 0xff;
  274. addr[2] = (bottom >> 16) & 0xff;
  275. addr[3] = (bottom >> 24) & 0xff;
  276. addr[4] = top & 0xff;
  277. addr[5] = (top >> 8) & 0xff;
  278. if (is_valid_ether_addr(addr)) {
  279. eth_hw_addr_set(bp->dev, addr);
  280. return;
  281. }
  282. }
  283. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  284. eth_hw_addr_random(bp->dev);
  285. }
  286. static int macb_mdio_wait_for_idle(struct macb *bp)
  287. {
  288. u32 val;
  289. return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
  290. 1, MACB_MDIO_TIMEOUT);
  291. }
  292. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  293. {
  294. struct macb *bp = bus->priv;
  295. int status;
  296. status = pm_runtime_resume_and_get(&bp->pdev->dev);
  297. if (status < 0)
  298. goto mdio_pm_exit;
  299. status = macb_mdio_wait_for_idle(bp);
  300. if (status < 0)
  301. goto mdio_read_exit;
  302. if (regnum & MII_ADDR_C45) {
  303. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  304. | MACB_BF(RW, MACB_MAN_C45_ADDR)
  305. | MACB_BF(PHYA, mii_id)
  306. | MACB_BF(REGA, (regnum >> 16) & 0x1F)
  307. | MACB_BF(DATA, regnum & 0xFFFF)
  308. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  309. status = macb_mdio_wait_for_idle(bp);
  310. if (status < 0)
  311. goto mdio_read_exit;
  312. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  313. | MACB_BF(RW, MACB_MAN_C45_READ)
  314. | MACB_BF(PHYA, mii_id)
  315. | MACB_BF(REGA, (regnum >> 16) & 0x1F)
  316. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  317. } else {
  318. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
  319. | MACB_BF(RW, MACB_MAN_C22_READ)
  320. | MACB_BF(PHYA, mii_id)
  321. | MACB_BF(REGA, regnum)
  322. | MACB_BF(CODE, MACB_MAN_C22_CODE)));
  323. }
  324. status = macb_mdio_wait_for_idle(bp);
  325. if (status < 0)
  326. goto mdio_read_exit;
  327. status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  328. mdio_read_exit:
  329. pm_runtime_mark_last_busy(&bp->pdev->dev);
  330. pm_runtime_put_autosuspend(&bp->pdev->dev);
  331. mdio_pm_exit:
  332. return status;
  333. }
  334. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  335. u16 value)
  336. {
  337. struct macb *bp = bus->priv;
  338. int status;
  339. status = pm_runtime_resume_and_get(&bp->pdev->dev);
  340. if (status < 0)
  341. goto mdio_pm_exit;
  342. status = macb_mdio_wait_for_idle(bp);
  343. if (status < 0)
  344. goto mdio_write_exit;
  345. if (regnum & MII_ADDR_C45) {
  346. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  347. | MACB_BF(RW, MACB_MAN_C45_ADDR)
  348. | MACB_BF(PHYA, mii_id)
  349. | MACB_BF(REGA, (regnum >> 16) & 0x1F)
  350. | MACB_BF(DATA, regnum & 0xFFFF)
  351. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  352. status = macb_mdio_wait_for_idle(bp);
  353. if (status < 0)
  354. goto mdio_write_exit;
  355. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  356. | MACB_BF(RW, MACB_MAN_C45_WRITE)
  357. | MACB_BF(PHYA, mii_id)
  358. | MACB_BF(REGA, (regnum >> 16) & 0x1F)
  359. | MACB_BF(CODE, MACB_MAN_C45_CODE)
  360. | MACB_BF(DATA, value)));
  361. } else {
  362. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
  363. | MACB_BF(RW, MACB_MAN_C22_WRITE)
  364. | MACB_BF(PHYA, mii_id)
  365. | MACB_BF(REGA, regnum)
  366. | MACB_BF(CODE, MACB_MAN_C22_CODE)
  367. | MACB_BF(DATA, value)));
  368. }
  369. status = macb_mdio_wait_for_idle(bp);
  370. if (status < 0)
  371. goto mdio_write_exit;
  372. mdio_write_exit:
  373. pm_runtime_mark_last_busy(&bp->pdev->dev);
  374. pm_runtime_put_autosuspend(&bp->pdev->dev);
  375. mdio_pm_exit:
  376. return status;
  377. }
  378. static void macb_init_buffers(struct macb *bp)
  379. {
  380. struct macb_queue *queue;
  381. unsigned int q;
  382. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  383. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  384. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  385. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  386. queue_writel(queue, RBQPH,
  387. upper_32_bits(queue->rx_ring_dma));
  388. #endif
  389. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  390. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  391. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  392. queue_writel(queue, TBQPH,
  393. upper_32_bits(queue->tx_ring_dma));
  394. #endif
  395. }
  396. }
  397. /**
  398. * macb_set_tx_clk() - Set a clock to a new frequency
  399. * @bp: pointer to struct macb
  400. * @speed: New frequency in Hz
  401. */
  402. static void macb_set_tx_clk(struct macb *bp, int speed)
  403. {
  404. long ferr, rate, rate_rounded;
  405. if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
  406. return;
  407. /* In case of MII the PHY is the clock master */
  408. if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
  409. return;
  410. switch (speed) {
  411. case SPEED_10:
  412. rate = 2500000;
  413. break;
  414. case SPEED_100:
  415. rate = 25000000;
  416. break;
  417. case SPEED_1000:
  418. rate = 125000000;
  419. break;
  420. default:
  421. return;
  422. }
  423. rate_rounded = clk_round_rate(bp->tx_clk, rate);
  424. if (rate_rounded < 0)
  425. return;
  426. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  427. * is not satisfied.
  428. */
  429. ferr = abs(rate_rounded - rate);
  430. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  431. if (ferr > 5)
  432. netdev_warn(bp->dev,
  433. "unable to generate target frequency: %ld Hz\n",
  434. rate);
  435. if (clk_set_rate(bp->tx_clk, rate_rounded))
  436. netdev_err(bp->dev, "adjusting tx_clk failed.\n");
  437. }
  438. static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode,
  439. phy_interface_t interface, int speed,
  440. int duplex)
  441. {
  442. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  443. u32 config;
  444. config = gem_readl(bp, USX_CONTROL);
  445. config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
  446. config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
  447. config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
  448. config |= GEM_BIT(TX_EN);
  449. gem_writel(bp, USX_CONTROL, config);
  450. }
  451. static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
  452. struct phylink_link_state *state)
  453. {
  454. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  455. u32 val;
  456. state->speed = SPEED_10000;
  457. state->duplex = 1;
  458. state->an_complete = 1;
  459. val = gem_readl(bp, USX_STATUS);
  460. state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
  461. val = gem_readl(bp, NCFGR);
  462. if (val & GEM_BIT(PAE))
  463. state->pause = MLO_PAUSE_RX;
  464. }
  465. static int macb_usx_pcs_config(struct phylink_pcs *pcs,
  466. unsigned int mode,
  467. phy_interface_t interface,
  468. const unsigned long *advertising,
  469. bool permit_pause_to_mac)
  470. {
  471. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  472. gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
  473. GEM_BIT(SIGNAL_OK));
  474. return 0;
  475. }
  476. static void macb_pcs_get_state(struct phylink_pcs *pcs,
  477. struct phylink_link_state *state)
  478. {
  479. state->link = 0;
  480. }
  481. static void macb_pcs_an_restart(struct phylink_pcs *pcs)
  482. {
  483. /* Not supported */
  484. }
  485. static int macb_pcs_config(struct phylink_pcs *pcs,
  486. unsigned int mode,
  487. phy_interface_t interface,
  488. const unsigned long *advertising,
  489. bool permit_pause_to_mac)
  490. {
  491. return 0;
  492. }
  493. static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
  494. .pcs_get_state = macb_usx_pcs_get_state,
  495. .pcs_config = macb_usx_pcs_config,
  496. .pcs_link_up = macb_usx_pcs_link_up,
  497. };
  498. static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
  499. .pcs_get_state = macb_pcs_get_state,
  500. .pcs_an_restart = macb_pcs_an_restart,
  501. .pcs_config = macb_pcs_config,
  502. };
  503. static void macb_mac_config(struct phylink_config *config, unsigned int mode,
  504. const struct phylink_link_state *state)
  505. {
  506. struct net_device *ndev = to_net_dev(config->dev);
  507. struct macb *bp = netdev_priv(ndev);
  508. unsigned long flags;
  509. u32 old_ctrl, ctrl;
  510. u32 old_ncr, ncr;
  511. spin_lock_irqsave(&bp->lock, flags);
  512. old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
  513. old_ncr = ncr = macb_or_gem_readl(bp, NCR);
  514. if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
  515. if (state->interface == PHY_INTERFACE_MODE_RMII)
  516. ctrl |= MACB_BIT(RM9200_RMII);
  517. } else if (macb_is_gem(bp)) {
  518. ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
  519. ncr &= ~GEM_BIT(ENABLE_HS_MAC);
  520. if (state->interface == PHY_INTERFACE_MODE_SGMII) {
  521. ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  522. } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
  523. ctrl |= GEM_BIT(PCSSEL);
  524. ncr |= GEM_BIT(ENABLE_HS_MAC);
  525. } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
  526. bp->phy_interface == PHY_INTERFACE_MODE_MII) {
  527. ncr |= MACB_BIT(MIIONRGMII);
  528. }
  529. }
  530. /* Apply the new configuration, if any */
  531. if (old_ctrl ^ ctrl)
  532. macb_or_gem_writel(bp, NCFGR, ctrl);
  533. if (old_ncr ^ ncr)
  534. macb_or_gem_writel(bp, NCR, ncr);
  535. /* Disable AN for SGMII fixed link configuration, enable otherwise.
  536. * Must be written after PCSSEL is set in NCFGR,
  537. * otherwise writes will not take effect.
  538. */
  539. if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
  540. u32 pcsctrl, old_pcsctrl;
  541. old_pcsctrl = gem_readl(bp, PCSCNTRL);
  542. if (mode == MLO_AN_FIXED)
  543. pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
  544. else
  545. pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
  546. if (old_pcsctrl != pcsctrl)
  547. gem_writel(bp, PCSCNTRL, pcsctrl);
  548. }
  549. spin_unlock_irqrestore(&bp->lock, flags);
  550. }
  551. static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
  552. phy_interface_t interface)
  553. {
  554. struct net_device *ndev = to_net_dev(config->dev);
  555. struct macb *bp = netdev_priv(ndev);
  556. struct macb_queue *queue;
  557. unsigned int q;
  558. u32 ctrl;
  559. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
  560. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  561. queue_writel(queue, IDR,
  562. bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
  563. /* Disable Rx and Tx */
  564. ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
  565. macb_writel(bp, NCR, ctrl);
  566. netif_tx_stop_all_queues(ndev);
  567. }
  568. static void macb_mac_link_up(struct phylink_config *config,
  569. struct phy_device *phy,
  570. unsigned int mode, phy_interface_t interface,
  571. int speed, int duplex,
  572. bool tx_pause, bool rx_pause)
  573. {
  574. struct net_device *ndev = to_net_dev(config->dev);
  575. struct macb *bp = netdev_priv(ndev);
  576. struct macb_queue *queue;
  577. unsigned long flags;
  578. unsigned int q;
  579. u32 ctrl;
  580. spin_lock_irqsave(&bp->lock, flags);
  581. ctrl = macb_or_gem_readl(bp, NCFGR);
  582. ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  583. if (speed == SPEED_100)
  584. ctrl |= MACB_BIT(SPD);
  585. if (duplex)
  586. ctrl |= MACB_BIT(FD);
  587. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
  588. ctrl &= ~MACB_BIT(PAE);
  589. if (macb_is_gem(bp)) {
  590. ctrl &= ~GEM_BIT(GBE);
  591. if (speed == SPEED_1000)
  592. ctrl |= GEM_BIT(GBE);
  593. }
  594. if (rx_pause)
  595. ctrl |= MACB_BIT(PAE);
  596. /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
  597. * cleared the pipeline and control registers.
  598. */
  599. bp->macbgem_ops.mog_init_rings(bp);
  600. macb_init_buffers(bp);
  601. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  602. queue_writel(queue, IER,
  603. bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
  604. }
  605. macb_or_gem_writel(bp, NCFGR, ctrl);
  606. if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
  607. gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
  608. gem_readl(bp, HS_MAC_CONFIG)));
  609. spin_unlock_irqrestore(&bp->lock, flags);
  610. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
  611. macb_set_tx_clk(bp, speed);
  612. /* Enable Rx and Tx; Enable PTP unicast */
  613. ctrl = macb_readl(bp, NCR);
  614. if (gem_has_ptp(bp))
  615. ctrl |= MACB_BIT(PTPUNI);
  616. macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
  617. netif_tx_wake_all_queues(ndev);
  618. }
  619. static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
  620. phy_interface_t interface)
  621. {
  622. struct net_device *ndev = to_net_dev(config->dev);
  623. struct macb *bp = netdev_priv(ndev);
  624. if (interface == PHY_INTERFACE_MODE_10GBASER)
  625. return &bp->phylink_usx_pcs;
  626. else if (interface == PHY_INTERFACE_MODE_SGMII)
  627. return &bp->phylink_sgmii_pcs;
  628. else
  629. return NULL;
  630. }
  631. static const struct phylink_mac_ops macb_phylink_ops = {
  632. .validate = phylink_generic_validate,
  633. .mac_select_pcs = macb_mac_select_pcs,
  634. .mac_config = macb_mac_config,
  635. .mac_link_down = macb_mac_link_down,
  636. .mac_link_up = macb_mac_link_up,
  637. };
  638. static bool macb_phy_handle_exists(struct device_node *dn)
  639. {
  640. dn = of_parse_phandle(dn, "phy-handle", 0);
  641. of_node_put(dn);
  642. return dn != NULL;
  643. }
  644. static int macb_phylink_connect(struct macb *bp)
  645. {
  646. struct device_node *dn = bp->pdev->dev.of_node;
  647. struct net_device *dev = bp->dev;
  648. struct phy_device *phydev;
  649. int ret;
  650. if (dn)
  651. ret = phylink_of_phy_connect(bp->phylink, dn, 0);
  652. if (!dn || (ret && !macb_phy_handle_exists(dn))) {
  653. phydev = phy_find_first(bp->mii_bus);
  654. if (!phydev) {
  655. netdev_err(dev, "no PHY found\n");
  656. return -ENXIO;
  657. }
  658. /* attach the mac to the phy */
  659. ret = phylink_connect_phy(bp->phylink, phydev);
  660. }
  661. if (ret) {
  662. netdev_err(dev, "Could not attach PHY (%d)\n", ret);
  663. return ret;
  664. }
  665. phylink_start(bp->phylink);
  666. return 0;
  667. }
  668. static void macb_get_pcs_fixed_state(struct phylink_config *config,
  669. struct phylink_link_state *state)
  670. {
  671. struct net_device *ndev = to_net_dev(config->dev);
  672. struct macb *bp = netdev_priv(ndev);
  673. state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
  674. }
  675. /* based on au1000_eth. c*/
  676. static int macb_mii_probe(struct net_device *dev)
  677. {
  678. struct macb *bp = netdev_priv(dev);
  679. bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops;
  680. bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops;
  681. bp->phylink_config.dev = &dev->dev;
  682. bp->phylink_config.type = PHYLINK_NETDEV;
  683. bp->phylink_config.mac_managed_pm = true;
  684. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  685. bp->phylink_config.poll_fixed_state = true;
  686. bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
  687. }
  688. bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
  689. MAC_10 | MAC_100;
  690. __set_bit(PHY_INTERFACE_MODE_MII,
  691. bp->phylink_config.supported_interfaces);
  692. __set_bit(PHY_INTERFACE_MODE_RMII,
  693. bp->phylink_config.supported_interfaces);
  694. /* Determine what modes are supported */
  695. if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) {
  696. bp->phylink_config.mac_capabilities |= MAC_1000FD;
  697. if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
  698. bp->phylink_config.mac_capabilities |= MAC_1000HD;
  699. __set_bit(PHY_INTERFACE_MODE_GMII,
  700. bp->phylink_config.supported_interfaces);
  701. phy_interface_set_rgmii(bp->phylink_config.supported_interfaces);
  702. if (bp->caps & MACB_CAPS_PCS)
  703. __set_bit(PHY_INTERFACE_MODE_SGMII,
  704. bp->phylink_config.supported_interfaces);
  705. if (bp->caps & MACB_CAPS_HIGH_SPEED) {
  706. __set_bit(PHY_INTERFACE_MODE_10GBASER,
  707. bp->phylink_config.supported_interfaces);
  708. bp->phylink_config.mac_capabilities |= MAC_10000FD;
  709. }
  710. }
  711. bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
  712. bp->phy_interface, &macb_phylink_ops);
  713. if (IS_ERR(bp->phylink)) {
  714. netdev_err(dev, "Could not create a phylink instance (%ld)\n",
  715. PTR_ERR(bp->phylink));
  716. return PTR_ERR(bp->phylink);
  717. }
  718. return 0;
  719. }
  720. static int macb_mdiobus_register(struct macb *bp)
  721. {
  722. struct device_node *child, *np = bp->pdev->dev.of_node;
  723. /* If we have a child named mdio, probe it instead of looking for PHYs
  724. * directly under the MAC node
  725. */
  726. child = of_get_child_by_name(np, "mdio");
  727. if (child) {
  728. int ret = of_mdiobus_register(bp->mii_bus, child);
  729. of_node_put(child);
  730. return ret;
  731. }
  732. if (of_phy_is_fixed_link(np))
  733. return mdiobus_register(bp->mii_bus);
  734. /* Only create the PHY from the device tree if at least one PHY is
  735. * described. Otherwise scan the entire MDIO bus. We do this to support
  736. * old device tree that did not follow the best practices and did not
  737. * describe their network PHYs.
  738. */
  739. for_each_available_child_of_node(np, child)
  740. if (of_mdiobus_child_is_phy(child)) {
  741. /* The loop increments the child refcount,
  742. * decrement it before returning.
  743. */
  744. of_node_put(child);
  745. return of_mdiobus_register(bp->mii_bus, np);
  746. }
  747. return mdiobus_register(bp->mii_bus);
  748. }
  749. static int macb_mii_init(struct macb *bp)
  750. {
  751. int err = -ENXIO;
  752. /* Enable management port */
  753. macb_writel(bp, NCR, MACB_BIT(MPE));
  754. bp->mii_bus = mdiobus_alloc();
  755. if (!bp->mii_bus) {
  756. err = -ENOMEM;
  757. goto err_out;
  758. }
  759. bp->mii_bus->name = "MACB_mii_bus";
  760. bp->mii_bus->read = &macb_mdio_read;
  761. bp->mii_bus->write = &macb_mdio_write;
  762. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  763. bp->pdev->name, bp->pdev->id);
  764. bp->mii_bus->priv = bp;
  765. bp->mii_bus->parent = &bp->pdev->dev;
  766. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  767. err = macb_mdiobus_register(bp);
  768. if (err)
  769. goto err_out_free_mdiobus;
  770. err = macb_mii_probe(bp->dev);
  771. if (err)
  772. goto err_out_unregister_bus;
  773. return 0;
  774. err_out_unregister_bus:
  775. mdiobus_unregister(bp->mii_bus);
  776. err_out_free_mdiobus:
  777. mdiobus_free(bp->mii_bus);
  778. err_out:
  779. return err;
  780. }
  781. static void macb_update_stats(struct macb *bp)
  782. {
  783. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  784. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  785. int offset = MACB_PFR;
  786. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  787. for (; p < end; p++, offset += 4)
  788. *p += bp->macb_reg_readl(bp, offset);
  789. }
  790. static int macb_halt_tx(struct macb *bp)
  791. {
  792. unsigned long halt_time, timeout;
  793. u32 status;
  794. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  795. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  796. do {
  797. halt_time = jiffies;
  798. status = macb_readl(bp, TSR);
  799. if (!(status & MACB_BIT(TGO)))
  800. return 0;
  801. udelay(250);
  802. } while (time_before(halt_time, timeout));
  803. return -ETIMEDOUT;
  804. }
  805. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb, int budget)
  806. {
  807. if (tx_skb->mapping) {
  808. if (tx_skb->mapped_as_page)
  809. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  810. tx_skb->size, DMA_TO_DEVICE);
  811. else
  812. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  813. tx_skb->size, DMA_TO_DEVICE);
  814. tx_skb->mapping = 0;
  815. }
  816. if (tx_skb->skb) {
  817. napi_consume_skb(tx_skb->skb, budget);
  818. tx_skb->skb = NULL;
  819. }
  820. }
  821. static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
  822. {
  823. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  824. struct macb_dma_desc_64 *desc_64;
  825. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  826. desc_64 = macb_64b_desc(bp, desc);
  827. desc_64->addrh = upper_32_bits(addr);
  828. /* The low bits of RX address contain the RX_USED bit, clearing
  829. * of which allows packet RX. Make sure the high bits are also
  830. * visible to HW at that point.
  831. */
  832. dma_wmb();
  833. }
  834. #endif
  835. desc->addr = lower_32_bits(addr);
  836. }
  837. static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
  838. {
  839. dma_addr_t addr = 0;
  840. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  841. struct macb_dma_desc_64 *desc_64;
  842. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  843. desc_64 = macb_64b_desc(bp, desc);
  844. addr = ((u64)(desc_64->addrh) << 32);
  845. }
  846. #endif
  847. addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  848. #ifdef CONFIG_MACB_USE_HWSTAMP
  849. if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
  850. addr &= ~GEM_BIT(DMA_RXVALID);
  851. #endif
  852. return addr;
  853. }
  854. static void macb_tx_error_task(struct work_struct *work)
  855. {
  856. struct macb_queue *queue = container_of(work, struct macb_queue,
  857. tx_error_task);
  858. struct macb *bp = queue->bp;
  859. struct macb_tx_skb *tx_skb;
  860. struct macb_dma_desc *desc;
  861. struct sk_buff *skb;
  862. unsigned int tail;
  863. unsigned long flags;
  864. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  865. (unsigned int)(queue - bp->queues),
  866. queue->tx_tail, queue->tx_head);
  867. /* Prevent the queue NAPI TX poll from running, as it calls
  868. * macb_tx_complete(), which in turn may call netif_wake_subqueue().
  869. * As explained below, we have to halt the transmission before updating
  870. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  871. * network engine about the macb/gem being halted.
  872. */
  873. napi_disable(&queue->napi_tx);
  874. spin_lock_irqsave(&bp->lock, flags);
  875. /* Make sure nobody is trying to queue up new packets */
  876. netif_tx_stop_all_queues(bp->dev);
  877. /* Stop transmission now
  878. * (in case we have just queued new packets)
  879. * macb/gem must be halted to write TBQP register
  880. */
  881. if (macb_halt_tx(bp))
  882. /* Just complain for now, reinitializing TX path can be good */
  883. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  884. /* Treat frames in TX queue including the ones that caused the error.
  885. * Free transmit buffers in upper layer.
  886. */
  887. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  888. u32 ctrl;
  889. desc = macb_tx_desc(queue, tail);
  890. ctrl = desc->ctrl;
  891. tx_skb = macb_tx_skb(queue, tail);
  892. skb = tx_skb->skb;
  893. if (ctrl & MACB_BIT(TX_USED)) {
  894. /* skb is set for the last buffer of the frame */
  895. while (!skb) {
  896. macb_tx_unmap(bp, tx_skb, 0);
  897. tail++;
  898. tx_skb = macb_tx_skb(queue, tail);
  899. skb = tx_skb->skb;
  900. }
  901. /* ctrl still refers to the first buffer descriptor
  902. * since it's the only one written back by the hardware
  903. */
  904. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  905. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  906. macb_tx_ring_wrap(bp, tail),
  907. skb->data);
  908. bp->dev->stats.tx_packets++;
  909. queue->stats.tx_packets++;
  910. bp->dev->stats.tx_bytes += skb->len;
  911. queue->stats.tx_bytes += skb->len;
  912. }
  913. } else {
  914. /* "Buffers exhausted mid-frame" errors may only happen
  915. * if the driver is buggy, so complain loudly about
  916. * those. Statistics are updated by hardware.
  917. */
  918. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  919. netdev_err(bp->dev,
  920. "BUG: TX buffers exhausted mid-frame\n");
  921. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  922. }
  923. macb_tx_unmap(bp, tx_skb, 0);
  924. }
  925. /* Set end of TX queue */
  926. desc = macb_tx_desc(queue, 0);
  927. macb_set_addr(bp, desc, 0);
  928. desc->ctrl = MACB_BIT(TX_USED);
  929. /* Make descriptor updates visible to hardware */
  930. wmb();
  931. /* Reinitialize the TX desc queue */
  932. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  933. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  934. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  935. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  936. #endif
  937. /* Make TX ring reflect state of hardware */
  938. queue->tx_head = 0;
  939. queue->tx_tail = 0;
  940. /* Housework before enabling TX IRQ */
  941. macb_writel(bp, TSR, macb_readl(bp, TSR));
  942. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  943. /* Now we are ready to start transmission again */
  944. netif_tx_start_all_queues(bp->dev);
  945. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  946. spin_unlock_irqrestore(&bp->lock, flags);
  947. napi_enable(&queue->napi_tx);
  948. }
  949. static bool ptp_one_step_sync(struct sk_buff *skb)
  950. {
  951. struct ptp_header *hdr;
  952. unsigned int ptp_class;
  953. u8 msgtype;
  954. /* No need to parse packet if PTP TS is not involved */
  955. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  956. goto not_oss;
  957. /* Identify and return whether PTP one step sync is being processed */
  958. ptp_class = ptp_classify_raw(skb);
  959. if (ptp_class == PTP_CLASS_NONE)
  960. goto not_oss;
  961. hdr = ptp_parse_header(skb, ptp_class);
  962. if (!hdr)
  963. goto not_oss;
  964. if (hdr->flag_field[0] & PTP_FLAG_TWOSTEP)
  965. goto not_oss;
  966. msgtype = ptp_get_msgtype(hdr, ptp_class);
  967. if (msgtype == PTP_MSGTYPE_SYNC)
  968. return true;
  969. not_oss:
  970. return false;
  971. }
  972. static int macb_tx_complete(struct macb_queue *queue, int budget)
  973. {
  974. struct macb *bp = queue->bp;
  975. u16 queue_index = queue - bp->queues;
  976. unsigned int tail;
  977. unsigned int head;
  978. int packets = 0;
  979. spin_lock(&queue->tx_ptr_lock);
  980. head = queue->tx_head;
  981. for (tail = queue->tx_tail; tail != head && packets < budget; tail++) {
  982. struct macb_tx_skb *tx_skb;
  983. struct sk_buff *skb;
  984. struct macb_dma_desc *desc;
  985. u32 ctrl;
  986. desc = macb_tx_desc(queue, tail);
  987. /* Make hw descriptor updates visible to CPU */
  988. rmb();
  989. ctrl = desc->ctrl;
  990. /* TX_USED bit is only set by hardware on the very first buffer
  991. * descriptor of the transmitted frame.
  992. */
  993. if (!(ctrl & MACB_BIT(TX_USED)))
  994. break;
  995. /* Process all buffers of the current transmitted frame */
  996. for (;; tail++) {
  997. tx_skb = macb_tx_skb(queue, tail);
  998. skb = tx_skb->skb;
  999. /* First, update TX stats if needed */
  1000. if (skb) {
  1001. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1002. !ptp_one_step_sync(skb) &&
  1003. gem_ptp_do_txstamp(queue, skb, desc) == 0) {
  1004. /* skb now belongs to timestamp buffer
  1005. * and will be removed later
  1006. */
  1007. tx_skb->skb = NULL;
  1008. }
  1009. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  1010. macb_tx_ring_wrap(bp, tail),
  1011. skb->data);
  1012. bp->dev->stats.tx_packets++;
  1013. queue->stats.tx_packets++;
  1014. bp->dev->stats.tx_bytes += skb->len;
  1015. queue->stats.tx_bytes += skb->len;
  1016. packets++;
  1017. }
  1018. /* Now we can safely release resources */
  1019. macb_tx_unmap(bp, tx_skb, budget);
  1020. /* skb is set only for the last buffer of the frame.
  1021. * WARNING: at this point skb has been freed by
  1022. * macb_tx_unmap().
  1023. */
  1024. if (skb)
  1025. break;
  1026. }
  1027. }
  1028. queue->tx_tail = tail;
  1029. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  1030. CIRC_CNT(queue->tx_head, queue->tx_tail,
  1031. bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
  1032. netif_wake_subqueue(bp->dev, queue_index);
  1033. spin_unlock(&queue->tx_ptr_lock);
  1034. return packets;
  1035. }
  1036. static void gem_rx_refill(struct macb_queue *queue)
  1037. {
  1038. unsigned int entry;
  1039. struct sk_buff *skb;
  1040. dma_addr_t paddr;
  1041. struct macb *bp = queue->bp;
  1042. struct macb_dma_desc *desc;
  1043. while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
  1044. bp->rx_ring_size) > 0) {
  1045. entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
  1046. /* Make hw descriptor updates visible to CPU */
  1047. rmb();
  1048. desc = macb_rx_desc(queue, entry);
  1049. if (!queue->rx_skbuff[entry]) {
  1050. /* allocate sk_buff for this free entry in ring */
  1051. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  1052. if (unlikely(!skb)) {
  1053. netdev_err(bp->dev,
  1054. "Unable to allocate sk_buff\n");
  1055. break;
  1056. }
  1057. /* now fill corresponding descriptor entry */
  1058. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  1059. bp->rx_buffer_size,
  1060. DMA_FROM_DEVICE);
  1061. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  1062. dev_kfree_skb(skb);
  1063. break;
  1064. }
  1065. queue->rx_skbuff[entry] = skb;
  1066. if (entry == bp->rx_ring_size - 1)
  1067. paddr |= MACB_BIT(RX_WRAP);
  1068. desc->ctrl = 0;
  1069. /* Setting addr clears RX_USED and allows reception,
  1070. * make sure ctrl is cleared first to avoid a race.
  1071. */
  1072. dma_wmb();
  1073. macb_set_addr(bp, desc, paddr);
  1074. /* properly align Ethernet header */
  1075. skb_reserve(skb, NET_IP_ALIGN);
  1076. } else {
  1077. desc->ctrl = 0;
  1078. dma_wmb();
  1079. desc->addr &= ~MACB_BIT(RX_USED);
  1080. }
  1081. queue->rx_prepared_head++;
  1082. }
  1083. /* Make descriptor updates visible to hardware */
  1084. wmb();
  1085. netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
  1086. queue, queue->rx_prepared_head, queue->rx_tail);
  1087. }
  1088. /* Mark DMA descriptors from begin up to and not including end as unused */
  1089. static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
  1090. unsigned int end)
  1091. {
  1092. unsigned int frag;
  1093. for (frag = begin; frag != end; frag++) {
  1094. struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
  1095. desc->addr &= ~MACB_BIT(RX_USED);
  1096. }
  1097. /* Make descriptor updates visible to hardware */
  1098. wmb();
  1099. /* When this happens, the hardware stats registers for
  1100. * whatever caused this is updated, so we don't have to record
  1101. * anything.
  1102. */
  1103. }
  1104. static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
  1105. int budget)
  1106. {
  1107. struct macb *bp = queue->bp;
  1108. unsigned int len;
  1109. unsigned int entry;
  1110. struct sk_buff *skb;
  1111. struct macb_dma_desc *desc;
  1112. int count = 0;
  1113. while (count < budget) {
  1114. u32 ctrl;
  1115. dma_addr_t addr;
  1116. bool rxused;
  1117. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  1118. desc = macb_rx_desc(queue, entry);
  1119. /* Make hw descriptor updates visible to CPU */
  1120. rmb();
  1121. rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
  1122. addr = macb_get_addr(bp, desc);
  1123. if (!rxused)
  1124. break;
  1125. /* Ensure ctrl is at least as up-to-date as rxused */
  1126. dma_rmb();
  1127. ctrl = desc->ctrl;
  1128. queue->rx_tail++;
  1129. count++;
  1130. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  1131. netdev_err(bp->dev,
  1132. "not whole frame pointed by descriptor\n");
  1133. bp->dev->stats.rx_dropped++;
  1134. queue->stats.rx_dropped++;
  1135. break;
  1136. }
  1137. skb = queue->rx_skbuff[entry];
  1138. if (unlikely(!skb)) {
  1139. netdev_err(bp->dev,
  1140. "inconsistent Rx descriptor chain\n");
  1141. bp->dev->stats.rx_dropped++;
  1142. queue->stats.rx_dropped++;
  1143. break;
  1144. }
  1145. /* now everything is ready for receiving packet */
  1146. queue->rx_skbuff[entry] = NULL;
  1147. len = ctrl & bp->rx_frm_len_mask;
  1148. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  1149. skb_put(skb, len);
  1150. dma_unmap_single(&bp->pdev->dev, addr,
  1151. bp->rx_buffer_size, DMA_FROM_DEVICE);
  1152. skb->protocol = eth_type_trans(skb, bp->dev);
  1153. skb_checksum_none_assert(skb);
  1154. if (bp->dev->features & NETIF_F_RXCSUM &&
  1155. !(bp->dev->flags & IFF_PROMISC) &&
  1156. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  1157. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1158. bp->dev->stats.rx_packets++;
  1159. queue->stats.rx_packets++;
  1160. bp->dev->stats.rx_bytes += skb->len;
  1161. queue->stats.rx_bytes += skb->len;
  1162. gem_ptp_do_rxstamp(bp, skb, desc);
  1163. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1164. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  1165. skb->len, skb->csum);
  1166. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  1167. skb_mac_header(skb), 16, true);
  1168. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  1169. skb->data, 32, true);
  1170. #endif
  1171. napi_gro_receive(napi, skb);
  1172. }
  1173. gem_rx_refill(queue);
  1174. return count;
  1175. }
  1176. static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
  1177. unsigned int first_frag, unsigned int last_frag)
  1178. {
  1179. unsigned int len;
  1180. unsigned int frag;
  1181. unsigned int offset;
  1182. struct sk_buff *skb;
  1183. struct macb_dma_desc *desc;
  1184. struct macb *bp = queue->bp;
  1185. desc = macb_rx_desc(queue, last_frag);
  1186. len = desc->ctrl & bp->rx_frm_len_mask;
  1187. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  1188. macb_rx_ring_wrap(bp, first_frag),
  1189. macb_rx_ring_wrap(bp, last_frag), len);
  1190. /* The ethernet header starts NET_IP_ALIGN bytes into the
  1191. * first buffer. Since the header is 14 bytes, this makes the
  1192. * payload word-aligned.
  1193. *
  1194. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  1195. * the two padding bytes into the skb so that we avoid hitting
  1196. * the slowpath in memcpy(), and pull them off afterwards.
  1197. */
  1198. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  1199. if (!skb) {
  1200. bp->dev->stats.rx_dropped++;
  1201. for (frag = first_frag; ; frag++) {
  1202. desc = macb_rx_desc(queue, frag);
  1203. desc->addr &= ~MACB_BIT(RX_USED);
  1204. if (frag == last_frag)
  1205. break;
  1206. }
  1207. /* Make descriptor updates visible to hardware */
  1208. wmb();
  1209. return 1;
  1210. }
  1211. offset = 0;
  1212. len += NET_IP_ALIGN;
  1213. skb_checksum_none_assert(skb);
  1214. skb_put(skb, len);
  1215. for (frag = first_frag; ; frag++) {
  1216. unsigned int frag_len = bp->rx_buffer_size;
  1217. if (offset + frag_len > len) {
  1218. if (unlikely(frag != last_frag)) {
  1219. dev_kfree_skb_any(skb);
  1220. return -1;
  1221. }
  1222. frag_len = len - offset;
  1223. }
  1224. skb_copy_to_linear_data_offset(skb, offset,
  1225. macb_rx_buffer(queue, frag),
  1226. frag_len);
  1227. offset += bp->rx_buffer_size;
  1228. desc = macb_rx_desc(queue, frag);
  1229. desc->addr &= ~MACB_BIT(RX_USED);
  1230. if (frag == last_frag)
  1231. break;
  1232. }
  1233. /* Make descriptor updates visible to hardware */
  1234. wmb();
  1235. __skb_pull(skb, NET_IP_ALIGN);
  1236. skb->protocol = eth_type_trans(skb, bp->dev);
  1237. bp->dev->stats.rx_packets++;
  1238. bp->dev->stats.rx_bytes += skb->len;
  1239. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  1240. skb->len, skb->csum);
  1241. napi_gro_receive(napi, skb);
  1242. return 0;
  1243. }
  1244. static inline void macb_init_rx_ring(struct macb_queue *queue)
  1245. {
  1246. struct macb *bp = queue->bp;
  1247. dma_addr_t addr;
  1248. struct macb_dma_desc *desc = NULL;
  1249. int i;
  1250. addr = queue->rx_buffers_dma;
  1251. for (i = 0; i < bp->rx_ring_size; i++) {
  1252. desc = macb_rx_desc(queue, i);
  1253. macb_set_addr(bp, desc, addr);
  1254. desc->ctrl = 0;
  1255. addr += bp->rx_buffer_size;
  1256. }
  1257. desc->addr |= MACB_BIT(RX_WRAP);
  1258. queue->rx_tail = 0;
  1259. }
  1260. static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
  1261. int budget)
  1262. {
  1263. struct macb *bp = queue->bp;
  1264. bool reset_rx_queue = false;
  1265. int received = 0;
  1266. unsigned int tail;
  1267. int first_frag = -1;
  1268. for (tail = queue->rx_tail; budget > 0; tail++) {
  1269. struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
  1270. u32 ctrl;
  1271. /* Make hw descriptor updates visible to CPU */
  1272. rmb();
  1273. if (!(desc->addr & MACB_BIT(RX_USED)))
  1274. break;
  1275. /* Ensure ctrl is at least as up-to-date as addr */
  1276. dma_rmb();
  1277. ctrl = desc->ctrl;
  1278. if (ctrl & MACB_BIT(RX_SOF)) {
  1279. if (first_frag != -1)
  1280. discard_partial_frame(queue, first_frag, tail);
  1281. first_frag = tail;
  1282. }
  1283. if (ctrl & MACB_BIT(RX_EOF)) {
  1284. int dropped;
  1285. if (unlikely(first_frag == -1)) {
  1286. reset_rx_queue = true;
  1287. continue;
  1288. }
  1289. dropped = macb_rx_frame(queue, napi, first_frag, tail);
  1290. first_frag = -1;
  1291. if (unlikely(dropped < 0)) {
  1292. reset_rx_queue = true;
  1293. continue;
  1294. }
  1295. if (!dropped) {
  1296. received++;
  1297. budget--;
  1298. }
  1299. }
  1300. }
  1301. if (unlikely(reset_rx_queue)) {
  1302. unsigned long flags;
  1303. u32 ctrl;
  1304. netdev_err(bp->dev, "RX queue corruption: reset it\n");
  1305. spin_lock_irqsave(&bp->lock, flags);
  1306. ctrl = macb_readl(bp, NCR);
  1307. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1308. macb_init_rx_ring(queue);
  1309. queue_writel(queue, RBQP, queue->rx_ring_dma);
  1310. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1311. spin_unlock_irqrestore(&bp->lock, flags);
  1312. return received;
  1313. }
  1314. if (first_frag != -1)
  1315. queue->rx_tail = first_frag;
  1316. else
  1317. queue->rx_tail = tail;
  1318. return received;
  1319. }
  1320. static bool macb_rx_pending(struct macb_queue *queue)
  1321. {
  1322. struct macb *bp = queue->bp;
  1323. unsigned int entry;
  1324. struct macb_dma_desc *desc;
  1325. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  1326. desc = macb_rx_desc(queue, entry);
  1327. /* Make hw descriptor updates visible to CPU */
  1328. rmb();
  1329. return (desc->addr & MACB_BIT(RX_USED)) != 0;
  1330. }
  1331. static int macb_rx_poll(struct napi_struct *napi, int budget)
  1332. {
  1333. struct macb_queue *queue = container_of(napi, struct macb_queue, napi_rx);
  1334. struct macb *bp = queue->bp;
  1335. int work_done;
  1336. work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
  1337. netdev_vdbg(bp->dev, "RX poll: queue = %u, work_done = %d, budget = %d\n",
  1338. (unsigned int)(queue - bp->queues), work_done, budget);
  1339. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1340. queue_writel(queue, IER, bp->rx_intr_mask);
  1341. /* Packet completions only seem to propagate to raise
  1342. * interrupts when interrupts are enabled at the time, so if
  1343. * packets were received while interrupts were disabled,
  1344. * they will not cause another interrupt to be generated when
  1345. * interrupts are re-enabled.
  1346. * Check for this case here to avoid losing a wakeup. This can
  1347. * potentially race with the interrupt handler doing the same
  1348. * actions if an interrupt is raised just after enabling them,
  1349. * but this should be harmless.
  1350. */
  1351. if (macb_rx_pending(queue)) {
  1352. queue_writel(queue, IDR, bp->rx_intr_mask);
  1353. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1354. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1355. netdev_vdbg(bp->dev, "poll: packets pending, reschedule\n");
  1356. napi_schedule(napi);
  1357. }
  1358. }
  1359. /* TODO: Handle errors */
  1360. return work_done;
  1361. }
  1362. static void macb_tx_restart(struct macb_queue *queue)
  1363. {
  1364. struct macb *bp = queue->bp;
  1365. unsigned int head_idx, tbqp;
  1366. spin_lock(&queue->tx_ptr_lock);
  1367. if (queue->tx_head == queue->tx_tail)
  1368. goto out_tx_ptr_unlock;
  1369. tbqp = queue_readl(queue, TBQP) / macb_dma_desc_get_size(bp);
  1370. tbqp = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, tbqp));
  1371. head_idx = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, queue->tx_head));
  1372. if (tbqp == head_idx)
  1373. goto out_tx_ptr_unlock;
  1374. spin_lock_irq(&bp->lock);
  1375. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1376. spin_unlock_irq(&bp->lock);
  1377. out_tx_ptr_unlock:
  1378. spin_unlock(&queue->tx_ptr_lock);
  1379. }
  1380. static bool macb_tx_complete_pending(struct macb_queue *queue)
  1381. {
  1382. bool retval = false;
  1383. spin_lock(&queue->tx_ptr_lock);
  1384. if (queue->tx_head != queue->tx_tail) {
  1385. /* Make hw descriptor updates visible to CPU */
  1386. rmb();
  1387. if (macb_tx_desc(queue, queue->tx_tail)->ctrl & MACB_BIT(TX_USED))
  1388. retval = true;
  1389. }
  1390. spin_unlock(&queue->tx_ptr_lock);
  1391. return retval;
  1392. }
  1393. static int macb_tx_poll(struct napi_struct *napi, int budget)
  1394. {
  1395. struct macb_queue *queue = container_of(napi, struct macb_queue, napi_tx);
  1396. struct macb *bp = queue->bp;
  1397. int work_done;
  1398. work_done = macb_tx_complete(queue, budget);
  1399. rmb(); // ensure txubr_pending is up to date
  1400. if (queue->txubr_pending) {
  1401. queue->txubr_pending = false;
  1402. netdev_vdbg(bp->dev, "poll: tx restart\n");
  1403. macb_tx_restart(queue);
  1404. }
  1405. netdev_vdbg(bp->dev, "TX poll: queue = %u, work_done = %d, budget = %d\n",
  1406. (unsigned int)(queue - bp->queues), work_done, budget);
  1407. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1408. queue_writel(queue, IER, MACB_BIT(TCOMP));
  1409. /* Packet completions only seem to propagate to raise
  1410. * interrupts when interrupts are enabled at the time, so if
  1411. * packets were sent while interrupts were disabled,
  1412. * they will not cause another interrupt to be generated when
  1413. * interrupts are re-enabled.
  1414. * Check for this case here to avoid losing a wakeup. This can
  1415. * potentially race with the interrupt handler doing the same
  1416. * actions if an interrupt is raised just after enabling them,
  1417. * but this should be harmless.
  1418. */
  1419. if (macb_tx_complete_pending(queue)) {
  1420. queue_writel(queue, IDR, MACB_BIT(TCOMP));
  1421. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1422. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  1423. netdev_vdbg(bp->dev, "TX poll: packets pending, reschedule\n");
  1424. napi_schedule(napi);
  1425. }
  1426. }
  1427. return work_done;
  1428. }
  1429. static void macb_hresp_error_task(struct tasklet_struct *t)
  1430. {
  1431. struct macb *bp = from_tasklet(bp, t, hresp_err_tasklet);
  1432. struct net_device *dev = bp->dev;
  1433. struct macb_queue *queue;
  1434. unsigned int q;
  1435. u32 ctrl;
  1436. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1437. queue_writel(queue, IDR, bp->rx_intr_mask |
  1438. MACB_TX_INT_FLAGS |
  1439. MACB_BIT(HRESP));
  1440. }
  1441. ctrl = macb_readl(bp, NCR);
  1442. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  1443. macb_writel(bp, NCR, ctrl);
  1444. netif_tx_stop_all_queues(dev);
  1445. netif_carrier_off(dev);
  1446. bp->macbgem_ops.mog_init_rings(bp);
  1447. /* Initialize TX and RX buffers */
  1448. macb_init_buffers(bp);
  1449. /* Enable interrupts */
  1450. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1451. queue_writel(queue, IER,
  1452. bp->rx_intr_mask |
  1453. MACB_TX_INT_FLAGS |
  1454. MACB_BIT(HRESP));
  1455. ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
  1456. macb_writel(bp, NCR, ctrl);
  1457. netif_carrier_on(dev);
  1458. netif_tx_start_all_queues(dev);
  1459. }
  1460. static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
  1461. {
  1462. struct macb_queue *queue = dev_id;
  1463. struct macb *bp = queue->bp;
  1464. u32 status;
  1465. status = queue_readl(queue, ISR);
  1466. if (unlikely(!status))
  1467. return IRQ_NONE;
  1468. spin_lock(&bp->lock);
  1469. if (status & MACB_BIT(WOL)) {
  1470. queue_writel(queue, IDR, MACB_BIT(WOL));
  1471. macb_writel(bp, WOL, 0);
  1472. netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
  1473. (unsigned int)(queue - bp->queues),
  1474. (unsigned long)status);
  1475. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1476. queue_writel(queue, ISR, MACB_BIT(WOL));
  1477. pm_wakeup_event(&bp->pdev->dev, 0);
  1478. }
  1479. spin_unlock(&bp->lock);
  1480. return IRQ_HANDLED;
  1481. }
  1482. static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
  1483. {
  1484. struct macb_queue *queue = dev_id;
  1485. struct macb *bp = queue->bp;
  1486. u32 status;
  1487. status = queue_readl(queue, ISR);
  1488. if (unlikely(!status))
  1489. return IRQ_NONE;
  1490. spin_lock(&bp->lock);
  1491. if (status & GEM_BIT(WOL)) {
  1492. queue_writel(queue, IDR, GEM_BIT(WOL));
  1493. gem_writel(bp, WOL, 0);
  1494. netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
  1495. (unsigned int)(queue - bp->queues),
  1496. (unsigned long)status);
  1497. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1498. queue_writel(queue, ISR, GEM_BIT(WOL));
  1499. pm_wakeup_event(&bp->pdev->dev, 0);
  1500. }
  1501. spin_unlock(&bp->lock);
  1502. return IRQ_HANDLED;
  1503. }
  1504. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  1505. {
  1506. struct macb_queue *queue = dev_id;
  1507. struct macb *bp = queue->bp;
  1508. struct net_device *dev = bp->dev;
  1509. u32 status, ctrl;
  1510. status = queue_readl(queue, ISR);
  1511. if (unlikely(!status))
  1512. return IRQ_NONE;
  1513. spin_lock(&bp->lock);
  1514. while (status) {
  1515. /* close possible race with dev_close */
  1516. if (unlikely(!netif_running(dev))) {
  1517. queue_writel(queue, IDR, -1);
  1518. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1519. queue_writel(queue, ISR, -1);
  1520. break;
  1521. }
  1522. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  1523. (unsigned int)(queue - bp->queues),
  1524. (unsigned long)status);
  1525. if (status & bp->rx_intr_mask) {
  1526. /* There's no point taking any more interrupts
  1527. * until we have processed the buffers. The
  1528. * scheduling call may fail if the poll routine
  1529. * is already scheduled, so disable interrupts
  1530. * now.
  1531. */
  1532. queue_writel(queue, IDR, bp->rx_intr_mask);
  1533. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1534. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1535. if (napi_schedule_prep(&queue->napi_rx)) {
  1536. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  1537. __napi_schedule(&queue->napi_rx);
  1538. }
  1539. }
  1540. if (status & (MACB_BIT(TCOMP) |
  1541. MACB_BIT(TXUBR))) {
  1542. queue_writel(queue, IDR, MACB_BIT(TCOMP));
  1543. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1544. queue_writel(queue, ISR, MACB_BIT(TCOMP) |
  1545. MACB_BIT(TXUBR));
  1546. if (status & MACB_BIT(TXUBR)) {
  1547. queue->txubr_pending = true;
  1548. wmb(); // ensure softirq can see update
  1549. }
  1550. if (napi_schedule_prep(&queue->napi_tx)) {
  1551. netdev_vdbg(bp->dev, "scheduling TX softirq\n");
  1552. __napi_schedule(&queue->napi_tx);
  1553. }
  1554. }
  1555. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  1556. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  1557. schedule_work(&queue->tx_error_task);
  1558. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1559. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  1560. break;
  1561. }
  1562. /* Link change detection isn't possible with RMII, so we'll
  1563. * add that if/when we get our hands on a full-blown MII PHY.
  1564. */
  1565. /* There is a hardware issue under heavy load where DMA can
  1566. * stop, this causes endless "used buffer descriptor read"
  1567. * interrupts but it can be cleared by re-enabling RX. See
  1568. * the at91rm9200 manual, section 41.3.1 or the Zynq manual
  1569. * section 16.7.4 for details. RXUBR is only enabled for
  1570. * these two versions.
  1571. */
  1572. if (status & MACB_BIT(RXUBR)) {
  1573. ctrl = macb_readl(bp, NCR);
  1574. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1575. wmb();
  1576. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1577. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1578. queue_writel(queue, ISR, MACB_BIT(RXUBR));
  1579. }
  1580. if (status & MACB_BIT(ISR_ROVR)) {
  1581. /* We missed at least one packet */
  1582. if (macb_is_gem(bp))
  1583. bp->hw_stats.gem.rx_overruns++;
  1584. else
  1585. bp->hw_stats.macb.rx_overruns++;
  1586. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1587. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  1588. }
  1589. if (status & MACB_BIT(HRESP)) {
  1590. tasklet_schedule(&bp->hresp_err_tasklet);
  1591. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  1592. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1593. queue_writel(queue, ISR, MACB_BIT(HRESP));
  1594. }
  1595. status = queue_readl(queue, ISR);
  1596. }
  1597. spin_unlock(&bp->lock);
  1598. return IRQ_HANDLED;
  1599. }
  1600. #ifdef CONFIG_NET_POLL_CONTROLLER
  1601. /* Polling receive - used by netconsole and other diagnostic tools
  1602. * to allow network i/o with interrupts disabled.
  1603. */
  1604. static void macb_poll_controller(struct net_device *dev)
  1605. {
  1606. struct macb *bp = netdev_priv(dev);
  1607. struct macb_queue *queue;
  1608. unsigned long flags;
  1609. unsigned int q;
  1610. local_irq_save(flags);
  1611. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1612. macb_interrupt(dev->irq, queue);
  1613. local_irq_restore(flags);
  1614. }
  1615. #endif
  1616. static unsigned int macb_tx_map(struct macb *bp,
  1617. struct macb_queue *queue,
  1618. struct sk_buff *skb,
  1619. unsigned int hdrlen)
  1620. {
  1621. dma_addr_t mapping;
  1622. unsigned int len, entry, i, tx_head = queue->tx_head;
  1623. struct macb_tx_skb *tx_skb = NULL;
  1624. struct macb_dma_desc *desc;
  1625. unsigned int offset, size, count = 0;
  1626. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  1627. unsigned int eof = 1, mss_mfs = 0;
  1628. u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
  1629. /* LSO */
  1630. if (skb_shinfo(skb)->gso_size != 0) {
  1631. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1632. /* UDP - UFO */
  1633. lso_ctrl = MACB_LSO_UFO_ENABLE;
  1634. else
  1635. /* TCP - TSO */
  1636. lso_ctrl = MACB_LSO_TSO_ENABLE;
  1637. }
  1638. /* First, map non-paged data */
  1639. len = skb_headlen(skb);
  1640. /* first buffer length */
  1641. size = hdrlen;
  1642. offset = 0;
  1643. while (len) {
  1644. entry = macb_tx_ring_wrap(bp, tx_head);
  1645. tx_skb = &queue->tx_skb[entry];
  1646. mapping = dma_map_single(&bp->pdev->dev,
  1647. skb->data + offset,
  1648. size, DMA_TO_DEVICE);
  1649. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1650. goto dma_error;
  1651. /* Save info to properly release resources */
  1652. tx_skb->skb = NULL;
  1653. tx_skb->mapping = mapping;
  1654. tx_skb->size = size;
  1655. tx_skb->mapped_as_page = false;
  1656. len -= size;
  1657. offset += size;
  1658. count++;
  1659. tx_head++;
  1660. size = min(len, bp->max_tx_length);
  1661. }
  1662. /* Then, map paged data from fragments */
  1663. for (f = 0; f < nr_frags; f++) {
  1664. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1665. len = skb_frag_size(frag);
  1666. offset = 0;
  1667. while (len) {
  1668. size = min(len, bp->max_tx_length);
  1669. entry = macb_tx_ring_wrap(bp, tx_head);
  1670. tx_skb = &queue->tx_skb[entry];
  1671. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  1672. offset, size, DMA_TO_DEVICE);
  1673. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1674. goto dma_error;
  1675. /* Save info to properly release resources */
  1676. tx_skb->skb = NULL;
  1677. tx_skb->mapping = mapping;
  1678. tx_skb->size = size;
  1679. tx_skb->mapped_as_page = true;
  1680. len -= size;
  1681. offset += size;
  1682. count++;
  1683. tx_head++;
  1684. }
  1685. }
  1686. /* Should never happen */
  1687. if (unlikely(!tx_skb)) {
  1688. netdev_err(bp->dev, "BUG! empty skb!\n");
  1689. return 0;
  1690. }
  1691. /* This is the last buffer of the frame: save socket buffer */
  1692. tx_skb->skb = skb;
  1693. /* Update TX ring: update buffer descriptors in reverse order
  1694. * to avoid race condition
  1695. */
  1696. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1697. * to set the end of TX queue
  1698. */
  1699. i = tx_head;
  1700. entry = macb_tx_ring_wrap(bp, i);
  1701. ctrl = MACB_BIT(TX_USED);
  1702. desc = macb_tx_desc(queue, entry);
  1703. desc->ctrl = ctrl;
  1704. if (lso_ctrl) {
  1705. if (lso_ctrl == MACB_LSO_UFO_ENABLE)
  1706. /* include header and FCS in value given to h/w */
  1707. mss_mfs = skb_shinfo(skb)->gso_size +
  1708. skb_transport_offset(skb) +
  1709. ETH_FCS_LEN;
  1710. else /* TSO */ {
  1711. mss_mfs = skb_shinfo(skb)->gso_size;
  1712. /* TCP Sequence Number Source Select
  1713. * can be set only for TSO
  1714. */
  1715. seq_ctrl = 0;
  1716. }
  1717. }
  1718. do {
  1719. i--;
  1720. entry = macb_tx_ring_wrap(bp, i);
  1721. tx_skb = &queue->tx_skb[entry];
  1722. desc = macb_tx_desc(queue, entry);
  1723. ctrl = (u32)tx_skb->size;
  1724. if (eof) {
  1725. ctrl |= MACB_BIT(TX_LAST);
  1726. eof = 0;
  1727. }
  1728. if (unlikely(entry == (bp->tx_ring_size - 1)))
  1729. ctrl |= MACB_BIT(TX_WRAP);
  1730. /* First descriptor is header descriptor */
  1731. if (i == queue->tx_head) {
  1732. ctrl |= MACB_BF(TX_LSO, lso_ctrl);
  1733. ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
  1734. if ((bp->dev->features & NETIF_F_HW_CSUM) &&
  1735. skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl &&
  1736. !ptp_one_step_sync(skb))
  1737. ctrl |= MACB_BIT(TX_NOCRC);
  1738. } else
  1739. /* Only set MSS/MFS on payload descriptors
  1740. * (second or later descriptor)
  1741. */
  1742. ctrl |= MACB_BF(MSS_MFS, mss_mfs);
  1743. /* Set TX buffer descriptor */
  1744. macb_set_addr(bp, desc, tx_skb->mapping);
  1745. /* desc->addr must be visible to hardware before clearing
  1746. * 'TX_USED' bit in desc->ctrl.
  1747. */
  1748. wmb();
  1749. desc->ctrl = ctrl;
  1750. } while (i != queue->tx_head);
  1751. queue->tx_head = tx_head;
  1752. return count;
  1753. dma_error:
  1754. netdev_err(bp->dev, "TX DMA map failed\n");
  1755. for (i = queue->tx_head; i != tx_head; i++) {
  1756. tx_skb = macb_tx_skb(queue, i);
  1757. macb_tx_unmap(bp, tx_skb, 0);
  1758. }
  1759. return 0;
  1760. }
  1761. static netdev_features_t macb_features_check(struct sk_buff *skb,
  1762. struct net_device *dev,
  1763. netdev_features_t features)
  1764. {
  1765. unsigned int nr_frags, f;
  1766. unsigned int hdrlen;
  1767. /* Validate LSO compatibility */
  1768. /* there is only one buffer or protocol is not UDP */
  1769. if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
  1770. return features;
  1771. /* length of header */
  1772. hdrlen = skb_transport_offset(skb);
  1773. /* For UFO only:
  1774. * When software supplies two or more payload buffers all payload buffers
  1775. * apart from the last must be a multiple of 8 bytes in size.
  1776. */
  1777. if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
  1778. return features & ~MACB_NETIF_LSO;
  1779. nr_frags = skb_shinfo(skb)->nr_frags;
  1780. /* No need to check last fragment */
  1781. nr_frags--;
  1782. for (f = 0; f < nr_frags; f++) {
  1783. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1784. if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
  1785. return features & ~MACB_NETIF_LSO;
  1786. }
  1787. return features;
  1788. }
  1789. static inline int macb_clear_csum(struct sk_buff *skb)
  1790. {
  1791. /* no change for packets without checksum offloading */
  1792. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1793. return 0;
  1794. /* make sure we can modify the header */
  1795. if (unlikely(skb_cow_head(skb, 0)))
  1796. return -1;
  1797. /* initialize checksum field
  1798. * This is required - at least for Zynq, which otherwise calculates
  1799. * wrong UDP header checksums for UDP packets with UDP data len <=2
  1800. */
  1801. *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
  1802. return 0;
  1803. }
  1804. static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
  1805. {
  1806. bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
  1807. skb_is_nonlinear(*skb);
  1808. int padlen = ETH_ZLEN - (*skb)->len;
  1809. int tailroom = skb_tailroom(*skb);
  1810. struct sk_buff *nskb;
  1811. u32 fcs;
  1812. if (!(ndev->features & NETIF_F_HW_CSUM) ||
  1813. !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
  1814. skb_shinfo(*skb)->gso_size || ptp_one_step_sync(*skb))
  1815. return 0;
  1816. if (padlen <= 0) {
  1817. /* FCS could be appeded to tailroom. */
  1818. if (tailroom >= ETH_FCS_LEN)
  1819. goto add_fcs;
  1820. /* No room for FCS, need to reallocate skb. */
  1821. else
  1822. padlen = ETH_FCS_LEN;
  1823. } else {
  1824. /* Add room for FCS. */
  1825. padlen += ETH_FCS_LEN;
  1826. }
  1827. if (cloned || tailroom < padlen) {
  1828. nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
  1829. if (!nskb)
  1830. return -ENOMEM;
  1831. dev_consume_skb_any(*skb);
  1832. *skb = nskb;
  1833. }
  1834. if (padlen > ETH_FCS_LEN)
  1835. skb_put_zero(*skb, padlen - ETH_FCS_LEN);
  1836. add_fcs:
  1837. /* set FCS to packet */
  1838. fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
  1839. fcs = ~fcs;
  1840. skb_put_u8(*skb, fcs & 0xff);
  1841. skb_put_u8(*skb, (fcs >> 8) & 0xff);
  1842. skb_put_u8(*skb, (fcs >> 16) & 0xff);
  1843. skb_put_u8(*skb, (fcs >> 24) & 0xff);
  1844. return 0;
  1845. }
  1846. static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1847. {
  1848. u16 queue_index = skb_get_queue_mapping(skb);
  1849. struct macb *bp = netdev_priv(dev);
  1850. struct macb_queue *queue = &bp->queues[queue_index];
  1851. unsigned int desc_cnt, nr_frags, frag_size, f;
  1852. unsigned int hdrlen;
  1853. bool is_lso;
  1854. netdev_tx_t ret = NETDEV_TX_OK;
  1855. if (macb_clear_csum(skb)) {
  1856. dev_kfree_skb_any(skb);
  1857. return ret;
  1858. }
  1859. if (macb_pad_and_fcs(&skb, dev)) {
  1860. dev_kfree_skb_any(skb);
  1861. return ret;
  1862. }
  1863. is_lso = (skb_shinfo(skb)->gso_size != 0);
  1864. if (is_lso) {
  1865. /* length of headers */
  1866. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1867. /* only queue eth + ip headers separately for UDP */
  1868. hdrlen = skb_transport_offset(skb);
  1869. else
  1870. hdrlen = skb_tcp_all_headers(skb);
  1871. if (skb_headlen(skb) < hdrlen) {
  1872. netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
  1873. /* if this is required, would need to copy to single buffer */
  1874. return NETDEV_TX_BUSY;
  1875. }
  1876. } else
  1877. hdrlen = min(skb_headlen(skb), bp->max_tx_length);
  1878. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1879. netdev_vdbg(bp->dev,
  1880. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1881. queue_index, skb->len, skb->head, skb->data,
  1882. skb_tail_pointer(skb), skb_end_pointer(skb));
  1883. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1884. skb->data, 16, true);
  1885. #endif
  1886. /* Count how many TX buffer descriptors are needed to send this
  1887. * socket buffer: skb fragments of jumbo frames may need to be
  1888. * split into many buffer descriptors.
  1889. */
  1890. if (is_lso && (skb_headlen(skb) > hdrlen))
  1891. /* extra header descriptor if also payload in first buffer */
  1892. desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
  1893. else
  1894. desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1895. nr_frags = skb_shinfo(skb)->nr_frags;
  1896. for (f = 0; f < nr_frags; f++) {
  1897. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1898. desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1899. }
  1900. spin_lock_bh(&queue->tx_ptr_lock);
  1901. /* This is a hard error, log it. */
  1902. if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
  1903. bp->tx_ring_size) < desc_cnt) {
  1904. netif_stop_subqueue(dev, queue_index);
  1905. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1906. queue->tx_head, queue->tx_tail);
  1907. ret = NETDEV_TX_BUSY;
  1908. goto unlock;
  1909. }
  1910. /* Map socket buffer for DMA transfer */
  1911. if (!macb_tx_map(bp, queue, skb, hdrlen)) {
  1912. dev_kfree_skb_any(skb);
  1913. goto unlock;
  1914. }
  1915. /* Make newly initialized descriptor visible to hardware */
  1916. wmb();
  1917. skb_tx_timestamp(skb);
  1918. spin_lock_irq(&bp->lock);
  1919. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1920. spin_unlock_irq(&bp->lock);
  1921. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
  1922. netif_stop_subqueue(dev, queue_index);
  1923. unlock:
  1924. spin_unlock_bh(&queue->tx_ptr_lock);
  1925. return ret;
  1926. }
  1927. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1928. {
  1929. if (!macb_is_gem(bp)) {
  1930. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1931. } else {
  1932. bp->rx_buffer_size = size;
  1933. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1934. netdev_dbg(bp->dev,
  1935. "RX buffer must be multiple of %d bytes, expanding\n",
  1936. RX_BUFFER_MULTIPLE);
  1937. bp->rx_buffer_size =
  1938. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1939. }
  1940. }
  1941. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
  1942. bp->dev->mtu, bp->rx_buffer_size);
  1943. }
  1944. static void gem_free_rx_buffers(struct macb *bp)
  1945. {
  1946. struct sk_buff *skb;
  1947. struct macb_dma_desc *desc;
  1948. struct macb_queue *queue;
  1949. dma_addr_t addr;
  1950. unsigned int q;
  1951. int i;
  1952. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1953. if (!queue->rx_skbuff)
  1954. continue;
  1955. for (i = 0; i < bp->rx_ring_size; i++) {
  1956. skb = queue->rx_skbuff[i];
  1957. if (!skb)
  1958. continue;
  1959. desc = macb_rx_desc(queue, i);
  1960. addr = macb_get_addr(bp, desc);
  1961. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1962. DMA_FROM_DEVICE);
  1963. dev_kfree_skb_any(skb);
  1964. skb = NULL;
  1965. }
  1966. kfree(queue->rx_skbuff);
  1967. queue->rx_skbuff = NULL;
  1968. }
  1969. }
  1970. static void macb_free_rx_buffers(struct macb *bp)
  1971. {
  1972. struct macb_queue *queue = &bp->queues[0];
  1973. if (queue->rx_buffers) {
  1974. dma_free_coherent(&bp->pdev->dev,
  1975. bp->rx_ring_size * bp->rx_buffer_size,
  1976. queue->rx_buffers, queue->rx_buffers_dma);
  1977. queue->rx_buffers = NULL;
  1978. }
  1979. }
  1980. static void macb_free_consistent(struct macb *bp)
  1981. {
  1982. struct macb_queue *queue;
  1983. unsigned int q;
  1984. int size;
  1985. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1986. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1987. kfree(queue->tx_skb);
  1988. queue->tx_skb = NULL;
  1989. if (queue->tx_ring) {
  1990. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  1991. dma_free_coherent(&bp->pdev->dev, size,
  1992. queue->tx_ring, queue->tx_ring_dma);
  1993. queue->tx_ring = NULL;
  1994. }
  1995. if (queue->rx_ring) {
  1996. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  1997. dma_free_coherent(&bp->pdev->dev, size,
  1998. queue->rx_ring, queue->rx_ring_dma);
  1999. queue->rx_ring = NULL;
  2000. }
  2001. }
  2002. }
  2003. static int gem_alloc_rx_buffers(struct macb *bp)
  2004. {
  2005. struct macb_queue *queue;
  2006. unsigned int q;
  2007. int size;
  2008. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2009. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  2010. queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
  2011. if (!queue->rx_skbuff)
  2012. return -ENOMEM;
  2013. else
  2014. netdev_dbg(bp->dev,
  2015. "Allocated %d RX struct sk_buff entries at %p\n",
  2016. bp->rx_ring_size, queue->rx_skbuff);
  2017. }
  2018. return 0;
  2019. }
  2020. static int macb_alloc_rx_buffers(struct macb *bp)
  2021. {
  2022. struct macb_queue *queue = &bp->queues[0];
  2023. int size;
  2024. size = bp->rx_ring_size * bp->rx_buffer_size;
  2025. queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  2026. &queue->rx_buffers_dma, GFP_KERNEL);
  2027. if (!queue->rx_buffers)
  2028. return -ENOMEM;
  2029. netdev_dbg(bp->dev,
  2030. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  2031. size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
  2032. return 0;
  2033. }
  2034. static int macb_alloc_consistent(struct macb *bp)
  2035. {
  2036. struct macb_queue *queue;
  2037. unsigned int q;
  2038. int size;
  2039. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2040. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  2041. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  2042. &queue->tx_ring_dma,
  2043. GFP_KERNEL);
  2044. if (!queue->tx_ring)
  2045. goto out_err;
  2046. netdev_dbg(bp->dev,
  2047. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  2048. q, size, (unsigned long)queue->tx_ring_dma,
  2049. queue->tx_ring);
  2050. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  2051. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  2052. if (!queue->tx_skb)
  2053. goto out_err;
  2054. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  2055. queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  2056. &queue->rx_ring_dma, GFP_KERNEL);
  2057. if (!queue->rx_ring)
  2058. goto out_err;
  2059. netdev_dbg(bp->dev,
  2060. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  2061. size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
  2062. }
  2063. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  2064. goto out_err;
  2065. return 0;
  2066. out_err:
  2067. macb_free_consistent(bp);
  2068. return -ENOMEM;
  2069. }
  2070. static void gem_init_rings(struct macb *bp)
  2071. {
  2072. struct macb_queue *queue;
  2073. struct macb_dma_desc *desc = NULL;
  2074. unsigned int q;
  2075. int i;
  2076. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2077. for (i = 0; i < bp->tx_ring_size; i++) {
  2078. desc = macb_tx_desc(queue, i);
  2079. macb_set_addr(bp, desc, 0);
  2080. desc->ctrl = MACB_BIT(TX_USED);
  2081. }
  2082. desc->ctrl |= MACB_BIT(TX_WRAP);
  2083. queue->tx_head = 0;
  2084. queue->tx_tail = 0;
  2085. queue->rx_tail = 0;
  2086. queue->rx_prepared_head = 0;
  2087. gem_rx_refill(queue);
  2088. }
  2089. }
  2090. static void macb_init_rings(struct macb *bp)
  2091. {
  2092. int i;
  2093. struct macb_dma_desc *desc = NULL;
  2094. macb_init_rx_ring(&bp->queues[0]);
  2095. for (i = 0; i < bp->tx_ring_size; i++) {
  2096. desc = macb_tx_desc(&bp->queues[0], i);
  2097. macb_set_addr(bp, desc, 0);
  2098. desc->ctrl = MACB_BIT(TX_USED);
  2099. }
  2100. bp->queues[0].tx_head = 0;
  2101. bp->queues[0].tx_tail = 0;
  2102. desc->ctrl |= MACB_BIT(TX_WRAP);
  2103. }
  2104. static void macb_reset_hw(struct macb *bp)
  2105. {
  2106. struct macb_queue *queue;
  2107. unsigned int q;
  2108. u32 ctrl = macb_readl(bp, NCR);
  2109. /* Disable RX and TX (XXX: Should we halt the transmission
  2110. * more gracefully?)
  2111. */
  2112. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  2113. /* Clear the stats registers (XXX: Update stats first?) */
  2114. ctrl |= MACB_BIT(CLRSTAT);
  2115. macb_writel(bp, NCR, ctrl);
  2116. /* Clear all status flags */
  2117. macb_writel(bp, TSR, -1);
  2118. macb_writel(bp, RSR, -1);
  2119. /* Disable all interrupts */
  2120. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2121. queue_writel(queue, IDR, -1);
  2122. queue_readl(queue, ISR);
  2123. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  2124. queue_writel(queue, ISR, -1);
  2125. }
  2126. }
  2127. static u32 gem_mdc_clk_div(struct macb *bp)
  2128. {
  2129. u32 config;
  2130. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  2131. if (pclk_hz <= 20000000)
  2132. config = GEM_BF(CLK, GEM_CLK_DIV8);
  2133. else if (pclk_hz <= 40000000)
  2134. config = GEM_BF(CLK, GEM_CLK_DIV16);
  2135. else if (pclk_hz <= 80000000)
  2136. config = GEM_BF(CLK, GEM_CLK_DIV32);
  2137. else if (pclk_hz <= 120000000)
  2138. config = GEM_BF(CLK, GEM_CLK_DIV48);
  2139. else if (pclk_hz <= 160000000)
  2140. config = GEM_BF(CLK, GEM_CLK_DIV64);
  2141. else
  2142. config = GEM_BF(CLK, GEM_CLK_DIV96);
  2143. return config;
  2144. }
  2145. static u32 macb_mdc_clk_div(struct macb *bp)
  2146. {
  2147. u32 config;
  2148. unsigned long pclk_hz;
  2149. if (macb_is_gem(bp))
  2150. return gem_mdc_clk_div(bp);
  2151. pclk_hz = clk_get_rate(bp->pclk);
  2152. if (pclk_hz <= 20000000)
  2153. config = MACB_BF(CLK, MACB_CLK_DIV8);
  2154. else if (pclk_hz <= 40000000)
  2155. config = MACB_BF(CLK, MACB_CLK_DIV16);
  2156. else if (pclk_hz <= 80000000)
  2157. config = MACB_BF(CLK, MACB_CLK_DIV32);
  2158. else
  2159. config = MACB_BF(CLK, MACB_CLK_DIV64);
  2160. return config;
  2161. }
  2162. /* Get the DMA bus width field of the network configuration register that we
  2163. * should program. We find the width from decoding the design configuration
  2164. * register to find the maximum supported data bus width.
  2165. */
  2166. static u32 macb_dbw(struct macb *bp)
  2167. {
  2168. if (!macb_is_gem(bp))
  2169. return 0;
  2170. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  2171. case 4:
  2172. return GEM_BF(DBW, GEM_DBW128);
  2173. case 2:
  2174. return GEM_BF(DBW, GEM_DBW64);
  2175. case 1:
  2176. default:
  2177. return GEM_BF(DBW, GEM_DBW32);
  2178. }
  2179. }
  2180. /* Configure the receive DMA engine
  2181. * - use the correct receive buffer size
  2182. * - set best burst length for DMA operations
  2183. * (if not supported by FIFO, it will fallback to default)
  2184. * - set both rx/tx packet buffers to full memory size
  2185. * These are configurable parameters for GEM.
  2186. */
  2187. static void macb_configure_dma(struct macb *bp)
  2188. {
  2189. struct macb_queue *queue;
  2190. u32 buffer_size;
  2191. unsigned int q;
  2192. u32 dmacfg;
  2193. buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
  2194. if (macb_is_gem(bp)) {
  2195. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  2196. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2197. if (q)
  2198. queue_writel(queue, RBQS, buffer_size);
  2199. else
  2200. dmacfg |= GEM_BF(RXBS, buffer_size);
  2201. }
  2202. if (bp->dma_burst_length)
  2203. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  2204. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  2205. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  2206. if (bp->native_io)
  2207. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  2208. else
  2209. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  2210. if (bp->dev->features & NETIF_F_HW_CSUM)
  2211. dmacfg |= GEM_BIT(TXCOEN);
  2212. else
  2213. dmacfg &= ~GEM_BIT(TXCOEN);
  2214. dmacfg &= ~GEM_BIT(ADDR64);
  2215. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2216. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  2217. dmacfg |= GEM_BIT(ADDR64);
  2218. #endif
  2219. #ifdef CONFIG_MACB_USE_HWSTAMP
  2220. if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
  2221. dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
  2222. #endif
  2223. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  2224. dmacfg);
  2225. gem_writel(bp, DMACFG, dmacfg);
  2226. }
  2227. }
  2228. static void macb_init_hw(struct macb *bp)
  2229. {
  2230. u32 config;
  2231. macb_reset_hw(bp);
  2232. macb_set_hwaddr(bp);
  2233. config = macb_mdc_clk_div(bp);
  2234. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  2235. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  2236. if (bp->caps & MACB_CAPS_JUMBO)
  2237. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  2238. else
  2239. config |= MACB_BIT(BIG); /* Receive oversized frames */
  2240. if (bp->dev->flags & IFF_PROMISC)
  2241. config |= MACB_BIT(CAF); /* Copy All Frames */
  2242. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  2243. config |= GEM_BIT(RXCOEN);
  2244. if (!(bp->dev->flags & IFF_BROADCAST))
  2245. config |= MACB_BIT(NBC); /* No BroadCast */
  2246. config |= macb_dbw(bp);
  2247. macb_writel(bp, NCFGR, config);
  2248. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  2249. gem_writel(bp, JML, bp->jumbo_max_len);
  2250. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  2251. if (bp->caps & MACB_CAPS_JUMBO)
  2252. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  2253. macb_configure_dma(bp);
  2254. }
  2255. /* The hash address register is 64 bits long and takes up two
  2256. * locations in the memory map. The least significant bits are stored
  2257. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  2258. *
  2259. * The unicast hash enable and the multicast hash enable bits in the
  2260. * network configuration register enable the reception of hash matched
  2261. * frames. The destination address is reduced to a 6 bit index into
  2262. * the 64 bit hash register using the following hash function. The
  2263. * hash function is an exclusive or of every sixth bit of the
  2264. * destination address.
  2265. *
  2266. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  2267. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  2268. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  2269. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  2270. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  2271. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  2272. *
  2273. * da[0] represents the least significant bit of the first byte
  2274. * received, that is, the multicast/unicast indicator, and da[47]
  2275. * represents the most significant bit of the last byte received. If
  2276. * the hash index, hi[n], points to a bit that is set in the hash
  2277. * register then the frame will be matched according to whether the
  2278. * frame is multicast or unicast. A multicast match will be signalled
  2279. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  2280. * index points to a bit set in the hash register. A unicast match
  2281. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  2282. * and the hash index points to a bit set in the hash register. To
  2283. * receive all multicast frames, the hash register should be set with
  2284. * all ones and the multicast hash enable bit should be set in the
  2285. * network configuration register.
  2286. */
  2287. static inline int hash_bit_value(int bitnr, __u8 *addr)
  2288. {
  2289. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  2290. return 1;
  2291. return 0;
  2292. }
  2293. /* Return the hash index value for the specified address. */
  2294. static int hash_get_index(__u8 *addr)
  2295. {
  2296. int i, j, bitval;
  2297. int hash_index = 0;
  2298. for (j = 0; j < 6; j++) {
  2299. for (i = 0, bitval = 0; i < 8; i++)
  2300. bitval ^= hash_bit_value(i * 6 + j, addr);
  2301. hash_index |= (bitval << j);
  2302. }
  2303. return hash_index;
  2304. }
  2305. /* Add multicast addresses to the internal multicast-hash table. */
  2306. static void macb_sethashtable(struct net_device *dev)
  2307. {
  2308. struct netdev_hw_addr *ha;
  2309. unsigned long mc_filter[2];
  2310. unsigned int bitnr;
  2311. struct macb *bp = netdev_priv(dev);
  2312. mc_filter[0] = 0;
  2313. mc_filter[1] = 0;
  2314. netdev_for_each_mc_addr(ha, dev) {
  2315. bitnr = hash_get_index(ha->addr);
  2316. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  2317. }
  2318. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  2319. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  2320. }
  2321. /* Enable/Disable promiscuous and multicast modes. */
  2322. static void macb_set_rx_mode(struct net_device *dev)
  2323. {
  2324. unsigned long cfg;
  2325. struct macb *bp = netdev_priv(dev);
  2326. cfg = macb_readl(bp, NCFGR);
  2327. if (dev->flags & IFF_PROMISC) {
  2328. /* Enable promiscuous mode */
  2329. cfg |= MACB_BIT(CAF);
  2330. /* Disable RX checksum offload */
  2331. if (macb_is_gem(bp))
  2332. cfg &= ~GEM_BIT(RXCOEN);
  2333. } else {
  2334. /* Disable promiscuous mode */
  2335. cfg &= ~MACB_BIT(CAF);
  2336. /* Enable RX checksum offload only if requested */
  2337. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  2338. cfg |= GEM_BIT(RXCOEN);
  2339. }
  2340. if (dev->flags & IFF_ALLMULTI) {
  2341. /* Enable all multicast mode */
  2342. macb_or_gem_writel(bp, HRB, -1);
  2343. macb_or_gem_writel(bp, HRT, -1);
  2344. cfg |= MACB_BIT(NCFGR_MTI);
  2345. } else if (!netdev_mc_empty(dev)) {
  2346. /* Enable specific multicasts */
  2347. macb_sethashtable(dev);
  2348. cfg |= MACB_BIT(NCFGR_MTI);
  2349. } else if (dev->flags & (~IFF_ALLMULTI)) {
  2350. /* Disable all multicast mode */
  2351. macb_or_gem_writel(bp, HRB, 0);
  2352. macb_or_gem_writel(bp, HRT, 0);
  2353. cfg &= ~MACB_BIT(NCFGR_MTI);
  2354. }
  2355. macb_writel(bp, NCFGR, cfg);
  2356. }
  2357. static int macb_open(struct net_device *dev)
  2358. {
  2359. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  2360. struct macb *bp = netdev_priv(dev);
  2361. struct macb_queue *queue;
  2362. unsigned int q;
  2363. int err;
  2364. netdev_dbg(bp->dev, "open\n");
  2365. err = pm_runtime_resume_and_get(&bp->pdev->dev);
  2366. if (err < 0)
  2367. return err;
  2368. /* RX buffers initialization */
  2369. macb_init_rx_buffer_size(bp, bufsz);
  2370. err = macb_alloc_consistent(bp);
  2371. if (err) {
  2372. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  2373. err);
  2374. goto pm_exit;
  2375. }
  2376. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2377. napi_enable(&queue->napi_rx);
  2378. napi_enable(&queue->napi_tx);
  2379. }
  2380. macb_init_hw(bp);
  2381. err = phy_power_on(bp->sgmii_phy);
  2382. if (err)
  2383. goto reset_hw;
  2384. err = macb_phylink_connect(bp);
  2385. if (err)
  2386. goto phy_off;
  2387. netif_tx_start_all_queues(dev);
  2388. if (bp->ptp_info)
  2389. bp->ptp_info->ptp_init(dev);
  2390. return 0;
  2391. phy_off:
  2392. phy_power_off(bp->sgmii_phy);
  2393. reset_hw:
  2394. macb_reset_hw(bp);
  2395. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2396. napi_disable(&queue->napi_rx);
  2397. napi_disable(&queue->napi_tx);
  2398. }
  2399. macb_free_consistent(bp);
  2400. pm_exit:
  2401. pm_runtime_put_sync(&bp->pdev->dev);
  2402. return err;
  2403. }
  2404. static int macb_close(struct net_device *dev)
  2405. {
  2406. struct macb *bp = netdev_priv(dev);
  2407. struct macb_queue *queue;
  2408. unsigned long flags;
  2409. unsigned int q;
  2410. netif_tx_stop_all_queues(dev);
  2411. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2412. napi_disable(&queue->napi_rx);
  2413. napi_disable(&queue->napi_tx);
  2414. }
  2415. phylink_stop(bp->phylink);
  2416. phylink_disconnect_phy(bp->phylink);
  2417. phy_power_off(bp->sgmii_phy);
  2418. spin_lock_irqsave(&bp->lock, flags);
  2419. macb_reset_hw(bp);
  2420. netif_carrier_off(dev);
  2421. spin_unlock_irqrestore(&bp->lock, flags);
  2422. macb_free_consistent(bp);
  2423. if (bp->ptp_info)
  2424. bp->ptp_info->ptp_remove(dev);
  2425. pm_runtime_put(&bp->pdev->dev);
  2426. return 0;
  2427. }
  2428. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  2429. {
  2430. if (netif_running(dev))
  2431. return -EBUSY;
  2432. dev->mtu = new_mtu;
  2433. return 0;
  2434. }
  2435. static void gem_update_stats(struct macb *bp)
  2436. {
  2437. struct macb_queue *queue;
  2438. unsigned int i, q, idx;
  2439. unsigned long *stat;
  2440. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  2441. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  2442. u32 offset = gem_statistics[i].offset;
  2443. u64 val = bp->macb_reg_readl(bp, offset);
  2444. bp->ethtool_stats[i] += val;
  2445. *p += val;
  2446. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  2447. /* Add GEM_OCTTXH, GEM_OCTRXH */
  2448. val = bp->macb_reg_readl(bp, offset + 4);
  2449. bp->ethtool_stats[i] += ((u64)val) << 32;
  2450. *(++p) += val;
  2451. }
  2452. }
  2453. idx = GEM_STATS_LEN;
  2454. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  2455. for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
  2456. bp->ethtool_stats[idx++] = *stat;
  2457. }
  2458. static struct net_device_stats *gem_get_stats(struct macb *bp)
  2459. {
  2460. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2461. struct net_device_stats *nstat = &bp->dev->stats;
  2462. if (!netif_running(bp->dev))
  2463. return nstat;
  2464. gem_update_stats(bp);
  2465. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  2466. hwstat->rx_alignment_errors +
  2467. hwstat->rx_resource_errors +
  2468. hwstat->rx_overruns +
  2469. hwstat->rx_oversize_frames +
  2470. hwstat->rx_jabbers +
  2471. hwstat->rx_undersized_frames +
  2472. hwstat->rx_length_field_frame_errors);
  2473. nstat->tx_errors = (hwstat->tx_late_collisions +
  2474. hwstat->tx_excessive_collisions +
  2475. hwstat->tx_underrun +
  2476. hwstat->tx_carrier_sense_errors);
  2477. nstat->multicast = hwstat->rx_multicast_frames;
  2478. nstat->collisions = (hwstat->tx_single_collision_frames +
  2479. hwstat->tx_multiple_collision_frames +
  2480. hwstat->tx_excessive_collisions);
  2481. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  2482. hwstat->rx_jabbers +
  2483. hwstat->rx_undersized_frames +
  2484. hwstat->rx_length_field_frame_errors);
  2485. nstat->rx_over_errors = hwstat->rx_resource_errors;
  2486. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  2487. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  2488. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2489. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  2490. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  2491. nstat->tx_fifo_errors = hwstat->tx_underrun;
  2492. return nstat;
  2493. }
  2494. static void gem_get_ethtool_stats(struct net_device *dev,
  2495. struct ethtool_stats *stats, u64 *data)
  2496. {
  2497. struct macb *bp;
  2498. bp = netdev_priv(dev);
  2499. gem_update_stats(bp);
  2500. memcpy(data, &bp->ethtool_stats, sizeof(u64)
  2501. * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
  2502. }
  2503. static int gem_get_sset_count(struct net_device *dev, int sset)
  2504. {
  2505. struct macb *bp = netdev_priv(dev);
  2506. switch (sset) {
  2507. case ETH_SS_STATS:
  2508. return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
  2509. default:
  2510. return -EOPNOTSUPP;
  2511. }
  2512. }
  2513. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  2514. {
  2515. char stat_string[ETH_GSTRING_LEN];
  2516. struct macb *bp = netdev_priv(dev);
  2517. struct macb_queue *queue;
  2518. unsigned int i;
  2519. unsigned int q;
  2520. switch (sset) {
  2521. case ETH_SS_STATS:
  2522. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  2523. memcpy(p, gem_statistics[i].stat_string,
  2524. ETH_GSTRING_LEN);
  2525. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2526. for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
  2527. snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
  2528. q, queue_statistics[i].stat_string);
  2529. memcpy(p, stat_string, ETH_GSTRING_LEN);
  2530. }
  2531. }
  2532. break;
  2533. }
  2534. }
  2535. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  2536. {
  2537. struct macb *bp = netdev_priv(dev);
  2538. struct net_device_stats *nstat = &bp->dev->stats;
  2539. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2540. if (macb_is_gem(bp))
  2541. return gem_get_stats(bp);
  2542. /* read stats from hardware */
  2543. macb_update_stats(bp);
  2544. /* Convert HW stats into netdevice stats */
  2545. nstat->rx_errors = (hwstat->rx_fcs_errors +
  2546. hwstat->rx_align_errors +
  2547. hwstat->rx_resource_errors +
  2548. hwstat->rx_overruns +
  2549. hwstat->rx_oversize_pkts +
  2550. hwstat->rx_jabbers +
  2551. hwstat->rx_undersize_pkts +
  2552. hwstat->rx_length_mismatch);
  2553. nstat->tx_errors = (hwstat->tx_late_cols +
  2554. hwstat->tx_excessive_cols +
  2555. hwstat->tx_underruns +
  2556. hwstat->tx_carrier_errors +
  2557. hwstat->sqe_test_errors);
  2558. nstat->collisions = (hwstat->tx_single_cols +
  2559. hwstat->tx_multiple_cols +
  2560. hwstat->tx_excessive_cols);
  2561. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  2562. hwstat->rx_jabbers +
  2563. hwstat->rx_undersize_pkts +
  2564. hwstat->rx_length_mismatch);
  2565. nstat->rx_over_errors = hwstat->rx_resource_errors +
  2566. hwstat->rx_overruns;
  2567. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  2568. nstat->rx_frame_errors = hwstat->rx_align_errors;
  2569. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2570. /* XXX: What does "missed" mean? */
  2571. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  2572. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  2573. nstat->tx_fifo_errors = hwstat->tx_underruns;
  2574. /* Don't know about heartbeat or window errors... */
  2575. return nstat;
  2576. }
  2577. static int macb_get_regs_len(struct net_device *netdev)
  2578. {
  2579. return MACB_GREGS_NBR * sizeof(u32);
  2580. }
  2581. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2582. void *p)
  2583. {
  2584. struct macb *bp = netdev_priv(dev);
  2585. unsigned int tail, head;
  2586. u32 *regs_buff = p;
  2587. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  2588. | MACB_GREGS_VERSION;
  2589. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  2590. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  2591. regs_buff[0] = macb_readl(bp, NCR);
  2592. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  2593. regs_buff[2] = macb_readl(bp, NSR);
  2594. regs_buff[3] = macb_readl(bp, TSR);
  2595. regs_buff[4] = macb_readl(bp, RBQP);
  2596. regs_buff[5] = macb_readl(bp, TBQP);
  2597. regs_buff[6] = macb_readl(bp, RSR);
  2598. regs_buff[7] = macb_readl(bp, IMR);
  2599. regs_buff[8] = tail;
  2600. regs_buff[9] = head;
  2601. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  2602. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  2603. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  2604. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  2605. if (macb_is_gem(bp))
  2606. regs_buff[13] = gem_readl(bp, DMACFG);
  2607. }
  2608. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2609. {
  2610. struct macb *bp = netdev_priv(netdev);
  2611. if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
  2612. phylink_ethtool_get_wol(bp->phylink, wol);
  2613. wol->supported |= WAKE_MAGIC;
  2614. if (bp->wol & MACB_WOL_ENABLED)
  2615. wol->wolopts |= WAKE_MAGIC;
  2616. }
  2617. }
  2618. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2619. {
  2620. struct macb *bp = netdev_priv(netdev);
  2621. int ret;
  2622. /* Pass the order to phylink layer */
  2623. ret = phylink_ethtool_set_wol(bp->phylink, wol);
  2624. /* Don't manage WoL on MAC if handled by the PHY
  2625. * or if there's a failure in talking to the PHY
  2626. */
  2627. if (!ret || ret != -EOPNOTSUPP)
  2628. return ret;
  2629. if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
  2630. (wol->wolopts & ~WAKE_MAGIC))
  2631. return -EOPNOTSUPP;
  2632. if (wol->wolopts & WAKE_MAGIC)
  2633. bp->wol |= MACB_WOL_ENABLED;
  2634. else
  2635. bp->wol &= ~MACB_WOL_ENABLED;
  2636. device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
  2637. return 0;
  2638. }
  2639. static int macb_get_link_ksettings(struct net_device *netdev,
  2640. struct ethtool_link_ksettings *kset)
  2641. {
  2642. struct macb *bp = netdev_priv(netdev);
  2643. return phylink_ethtool_ksettings_get(bp->phylink, kset);
  2644. }
  2645. static int macb_set_link_ksettings(struct net_device *netdev,
  2646. const struct ethtool_link_ksettings *kset)
  2647. {
  2648. struct macb *bp = netdev_priv(netdev);
  2649. return phylink_ethtool_ksettings_set(bp->phylink, kset);
  2650. }
  2651. static void macb_get_ringparam(struct net_device *netdev,
  2652. struct ethtool_ringparam *ring,
  2653. struct kernel_ethtool_ringparam *kernel_ring,
  2654. struct netlink_ext_ack *extack)
  2655. {
  2656. struct macb *bp = netdev_priv(netdev);
  2657. ring->rx_max_pending = MAX_RX_RING_SIZE;
  2658. ring->tx_max_pending = MAX_TX_RING_SIZE;
  2659. ring->rx_pending = bp->rx_ring_size;
  2660. ring->tx_pending = bp->tx_ring_size;
  2661. }
  2662. static int macb_set_ringparam(struct net_device *netdev,
  2663. struct ethtool_ringparam *ring,
  2664. struct kernel_ethtool_ringparam *kernel_ring,
  2665. struct netlink_ext_ack *extack)
  2666. {
  2667. struct macb *bp = netdev_priv(netdev);
  2668. u32 new_rx_size, new_tx_size;
  2669. unsigned int reset = 0;
  2670. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2671. return -EINVAL;
  2672. new_rx_size = clamp_t(u32, ring->rx_pending,
  2673. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  2674. new_rx_size = roundup_pow_of_two(new_rx_size);
  2675. new_tx_size = clamp_t(u32, ring->tx_pending,
  2676. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  2677. new_tx_size = roundup_pow_of_two(new_tx_size);
  2678. if ((new_tx_size == bp->tx_ring_size) &&
  2679. (new_rx_size == bp->rx_ring_size)) {
  2680. /* nothing to do */
  2681. return 0;
  2682. }
  2683. if (netif_running(bp->dev)) {
  2684. reset = 1;
  2685. macb_close(bp->dev);
  2686. }
  2687. bp->rx_ring_size = new_rx_size;
  2688. bp->tx_ring_size = new_tx_size;
  2689. if (reset)
  2690. macb_open(bp->dev);
  2691. return 0;
  2692. }
  2693. #ifdef CONFIG_MACB_USE_HWSTAMP
  2694. static unsigned int gem_get_tsu_rate(struct macb *bp)
  2695. {
  2696. struct clk *tsu_clk;
  2697. unsigned int tsu_rate;
  2698. tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
  2699. if (!IS_ERR(tsu_clk))
  2700. tsu_rate = clk_get_rate(tsu_clk);
  2701. /* try pclk instead */
  2702. else if (!IS_ERR(bp->pclk)) {
  2703. tsu_clk = bp->pclk;
  2704. tsu_rate = clk_get_rate(tsu_clk);
  2705. } else
  2706. return -ENOTSUPP;
  2707. return tsu_rate;
  2708. }
  2709. static s32 gem_get_ptp_max_adj(void)
  2710. {
  2711. return 64000000;
  2712. }
  2713. static int gem_get_ts_info(struct net_device *dev,
  2714. struct ethtool_ts_info *info)
  2715. {
  2716. struct macb *bp = netdev_priv(dev);
  2717. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
  2718. ethtool_op_get_ts_info(dev, info);
  2719. return 0;
  2720. }
  2721. info->so_timestamping =
  2722. SOF_TIMESTAMPING_TX_SOFTWARE |
  2723. SOF_TIMESTAMPING_RX_SOFTWARE |
  2724. SOF_TIMESTAMPING_SOFTWARE |
  2725. SOF_TIMESTAMPING_TX_HARDWARE |
  2726. SOF_TIMESTAMPING_RX_HARDWARE |
  2727. SOF_TIMESTAMPING_RAW_HARDWARE;
  2728. info->tx_types =
  2729. (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
  2730. (1 << HWTSTAMP_TX_OFF) |
  2731. (1 << HWTSTAMP_TX_ON);
  2732. info->rx_filters =
  2733. (1 << HWTSTAMP_FILTER_NONE) |
  2734. (1 << HWTSTAMP_FILTER_ALL);
  2735. info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
  2736. return 0;
  2737. }
  2738. static struct macb_ptp_info gem_ptp_info = {
  2739. .ptp_init = gem_ptp_init,
  2740. .ptp_remove = gem_ptp_remove,
  2741. .get_ptp_max_adj = gem_get_ptp_max_adj,
  2742. .get_tsu_rate = gem_get_tsu_rate,
  2743. .get_ts_info = gem_get_ts_info,
  2744. .get_hwtst = gem_get_hwtst,
  2745. .set_hwtst = gem_set_hwtst,
  2746. };
  2747. #endif
  2748. static int macb_get_ts_info(struct net_device *netdev,
  2749. struct ethtool_ts_info *info)
  2750. {
  2751. struct macb *bp = netdev_priv(netdev);
  2752. if (bp->ptp_info)
  2753. return bp->ptp_info->get_ts_info(netdev, info);
  2754. return ethtool_op_get_ts_info(netdev, info);
  2755. }
  2756. static void gem_enable_flow_filters(struct macb *bp, bool enable)
  2757. {
  2758. struct net_device *netdev = bp->dev;
  2759. struct ethtool_rx_fs_item *item;
  2760. u32 t2_scr;
  2761. int num_t2_scr;
  2762. if (!(netdev->features & NETIF_F_NTUPLE))
  2763. return;
  2764. num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
  2765. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2766. struct ethtool_rx_flow_spec *fs = &item->fs;
  2767. struct ethtool_tcpip4_spec *tp4sp_m;
  2768. if (fs->location >= num_t2_scr)
  2769. continue;
  2770. t2_scr = gem_readl_n(bp, SCRT2, fs->location);
  2771. /* enable/disable screener regs for the flow entry */
  2772. t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
  2773. /* only enable fields with no masking */
  2774. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2775. if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
  2776. t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
  2777. else
  2778. t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
  2779. if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
  2780. t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
  2781. else
  2782. t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
  2783. if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
  2784. t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
  2785. else
  2786. t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
  2787. gem_writel_n(bp, SCRT2, fs->location, t2_scr);
  2788. }
  2789. }
  2790. static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
  2791. {
  2792. struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
  2793. uint16_t index = fs->location;
  2794. u32 w0, w1, t2_scr;
  2795. bool cmp_a = false;
  2796. bool cmp_b = false;
  2797. bool cmp_c = false;
  2798. if (!macb_is_gem(bp))
  2799. return;
  2800. tp4sp_v = &(fs->h_u.tcp_ip4_spec);
  2801. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2802. /* ignore field if any masking set */
  2803. if (tp4sp_m->ip4src == 0xFFFFFFFF) {
  2804. /* 1st compare reg - IP source address */
  2805. w0 = 0;
  2806. w1 = 0;
  2807. w0 = tp4sp_v->ip4src;
  2808. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2809. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2810. w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
  2811. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
  2812. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
  2813. cmp_a = true;
  2814. }
  2815. /* ignore field if any masking set */
  2816. if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
  2817. /* 2nd compare reg - IP destination address */
  2818. w0 = 0;
  2819. w1 = 0;
  2820. w0 = tp4sp_v->ip4dst;
  2821. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2822. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2823. w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
  2824. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
  2825. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
  2826. cmp_b = true;
  2827. }
  2828. /* ignore both port fields if masking set in both */
  2829. if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
  2830. /* 3rd compare reg - source port, destination port */
  2831. w0 = 0;
  2832. w1 = 0;
  2833. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
  2834. if (tp4sp_m->psrc == tp4sp_m->pdst) {
  2835. w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
  2836. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2837. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2838. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2839. } else {
  2840. /* only one port definition */
  2841. w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
  2842. w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
  2843. if (tp4sp_m->psrc == 0xFFFF) { /* src port */
  2844. w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
  2845. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2846. } else { /* dst port */
  2847. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2848. w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
  2849. }
  2850. }
  2851. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
  2852. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
  2853. cmp_c = true;
  2854. }
  2855. t2_scr = 0;
  2856. t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
  2857. t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
  2858. if (cmp_a)
  2859. t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
  2860. if (cmp_b)
  2861. t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
  2862. if (cmp_c)
  2863. t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
  2864. gem_writel_n(bp, SCRT2, index, t2_scr);
  2865. }
  2866. static int gem_add_flow_filter(struct net_device *netdev,
  2867. struct ethtool_rxnfc *cmd)
  2868. {
  2869. struct macb *bp = netdev_priv(netdev);
  2870. struct ethtool_rx_flow_spec *fs = &cmd->fs;
  2871. struct ethtool_rx_fs_item *item, *newfs;
  2872. unsigned long flags;
  2873. int ret = -EINVAL;
  2874. bool added = false;
  2875. newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
  2876. if (newfs == NULL)
  2877. return -ENOMEM;
  2878. memcpy(&newfs->fs, fs, sizeof(newfs->fs));
  2879. netdev_dbg(netdev,
  2880. "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2881. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2882. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2883. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2884. be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
  2885. be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
  2886. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2887. /* find correct place to add in list */
  2888. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2889. if (item->fs.location > newfs->fs.location) {
  2890. list_add_tail(&newfs->list, &item->list);
  2891. added = true;
  2892. break;
  2893. } else if (item->fs.location == fs->location) {
  2894. netdev_err(netdev, "Rule not added: location %d not free!\n",
  2895. fs->location);
  2896. ret = -EBUSY;
  2897. goto err;
  2898. }
  2899. }
  2900. if (!added)
  2901. list_add_tail(&newfs->list, &bp->rx_fs_list.list);
  2902. gem_prog_cmp_regs(bp, fs);
  2903. bp->rx_fs_list.count++;
  2904. /* enable filtering if NTUPLE on */
  2905. gem_enable_flow_filters(bp, 1);
  2906. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2907. return 0;
  2908. err:
  2909. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2910. kfree(newfs);
  2911. return ret;
  2912. }
  2913. static int gem_del_flow_filter(struct net_device *netdev,
  2914. struct ethtool_rxnfc *cmd)
  2915. {
  2916. struct macb *bp = netdev_priv(netdev);
  2917. struct ethtool_rx_fs_item *item;
  2918. struct ethtool_rx_flow_spec *fs;
  2919. unsigned long flags;
  2920. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2921. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2922. if (item->fs.location == cmd->fs.location) {
  2923. /* disable screener regs for the flow entry */
  2924. fs = &(item->fs);
  2925. netdev_dbg(netdev,
  2926. "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2927. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2928. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2929. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2930. be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
  2931. be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
  2932. gem_writel_n(bp, SCRT2, fs->location, 0);
  2933. list_del(&item->list);
  2934. bp->rx_fs_list.count--;
  2935. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2936. kfree(item);
  2937. return 0;
  2938. }
  2939. }
  2940. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2941. return -EINVAL;
  2942. }
  2943. static int gem_get_flow_entry(struct net_device *netdev,
  2944. struct ethtool_rxnfc *cmd)
  2945. {
  2946. struct macb *bp = netdev_priv(netdev);
  2947. struct ethtool_rx_fs_item *item;
  2948. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2949. if (item->fs.location == cmd->fs.location) {
  2950. memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
  2951. return 0;
  2952. }
  2953. }
  2954. return -EINVAL;
  2955. }
  2956. static int gem_get_all_flow_entries(struct net_device *netdev,
  2957. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  2958. {
  2959. struct macb *bp = netdev_priv(netdev);
  2960. struct ethtool_rx_fs_item *item;
  2961. uint32_t cnt = 0;
  2962. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2963. if (cnt == cmd->rule_cnt)
  2964. return -EMSGSIZE;
  2965. rule_locs[cnt] = item->fs.location;
  2966. cnt++;
  2967. }
  2968. cmd->data = bp->max_tuples;
  2969. cmd->rule_cnt = cnt;
  2970. return 0;
  2971. }
  2972. static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  2973. u32 *rule_locs)
  2974. {
  2975. struct macb *bp = netdev_priv(netdev);
  2976. int ret = 0;
  2977. switch (cmd->cmd) {
  2978. case ETHTOOL_GRXRINGS:
  2979. cmd->data = bp->num_queues;
  2980. break;
  2981. case ETHTOOL_GRXCLSRLCNT:
  2982. cmd->rule_cnt = bp->rx_fs_list.count;
  2983. break;
  2984. case ETHTOOL_GRXCLSRULE:
  2985. ret = gem_get_flow_entry(netdev, cmd);
  2986. break;
  2987. case ETHTOOL_GRXCLSRLALL:
  2988. ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
  2989. break;
  2990. default:
  2991. netdev_err(netdev,
  2992. "Command parameter %d is not supported\n", cmd->cmd);
  2993. ret = -EOPNOTSUPP;
  2994. }
  2995. return ret;
  2996. }
  2997. static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  2998. {
  2999. struct macb *bp = netdev_priv(netdev);
  3000. int ret;
  3001. switch (cmd->cmd) {
  3002. case ETHTOOL_SRXCLSRLINS:
  3003. if ((cmd->fs.location >= bp->max_tuples)
  3004. || (cmd->fs.ring_cookie >= bp->num_queues)) {
  3005. ret = -EINVAL;
  3006. break;
  3007. }
  3008. ret = gem_add_flow_filter(netdev, cmd);
  3009. break;
  3010. case ETHTOOL_SRXCLSRLDEL:
  3011. ret = gem_del_flow_filter(netdev, cmd);
  3012. break;
  3013. default:
  3014. netdev_err(netdev,
  3015. "Command parameter %d is not supported\n", cmd->cmd);
  3016. ret = -EOPNOTSUPP;
  3017. }
  3018. return ret;
  3019. }
  3020. static const struct ethtool_ops macb_ethtool_ops = {
  3021. .get_regs_len = macb_get_regs_len,
  3022. .get_regs = macb_get_regs,
  3023. .get_link = ethtool_op_get_link,
  3024. .get_ts_info = ethtool_op_get_ts_info,
  3025. .get_wol = macb_get_wol,
  3026. .set_wol = macb_set_wol,
  3027. .get_link_ksettings = macb_get_link_ksettings,
  3028. .set_link_ksettings = macb_set_link_ksettings,
  3029. .get_ringparam = macb_get_ringparam,
  3030. .set_ringparam = macb_set_ringparam,
  3031. };
  3032. static const struct ethtool_ops gem_ethtool_ops = {
  3033. .get_regs_len = macb_get_regs_len,
  3034. .get_regs = macb_get_regs,
  3035. .get_wol = macb_get_wol,
  3036. .set_wol = macb_set_wol,
  3037. .get_link = ethtool_op_get_link,
  3038. .get_ts_info = macb_get_ts_info,
  3039. .get_ethtool_stats = gem_get_ethtool_stats,
  3040. .get_strings = gem_get_ethtool_strings,
  3041. .get_sset_count = gem_get_sset_count,
  3042. .get_link_ksettings = macb_get_link_ksettings,
  3043. .set_link_ksettings = macb_set_link_ksettings,
  3044. .get_ringparam = macb_get_ringparam,
  3045. .set_ringparam = macb_set_ringparam,
  3046. .get_rxnfc = gem_get_rxnfc,
  3047. .set_rxnfc = gem_set_rxnfc,
  3048. };
  3049. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  3050. {
  3051. struct macb *bp = netdev_priv(dev);
  3052. if (!netif_running(dev))
  3053. return -EINVAL;
  3054. if (bp->ptp_info) {
  3055. switch (cmd) {
  3056. case SIOCSHWTSTAMP:
  3057. return bp->ptp_info->set_hwtst(dev, rq, cmd);
  3058. case SIOCGHWTSTAMP:
  3059. return bp->ptp_info->get_hwtst(dev, rq);
  3060. }
  3061. }
  3062. return phylink_mii_ioctl(bp->phylink, rq, cmd);
  3063. }
  3064. static inline void macb_set_txcsum_feature(struct macb *bp,
  3065. netdev_features_t features)
  3066. {
  3067. u32 val;
  3068. if (!macb_is_gem(bp))
  3069. return;
  3070. val = gem_readl(bp, DMACFG);
  3071. if (features & NETIF_F_HW_CSUM)
  3072. val |= GEM_BIT(TXCOEN);
  3073. else
  3074. val &= ~GEM_BIT(TXCOEN);
  3075. gem_writel(bp, DMACFG, val);
  3076. }
  3077. static inline void macb_set_rxcsum_feature(struct macb *bp,
  3078. netdev_features_t features)
  3079. {
  3080. struct net_device *netdev = bp->dev;
  3081. u32 val;
  3082. if (!macb_is_gem(bp))
  3083. return;
  3084. val = gem_readl(bp, NCFGR);
  3085. if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
  3086. val |= GEM_BIT(RXCOEN);
  3087. else
  3088. val &= ~GEM_BIT(RXCOEN);
  3089. gem_writel(bp, NCFGR, val);
  3090. }
  3091. static inline void macb_set_rxflow_feature(struct macb *bp,
  3092. netdev_features_t features)
  3093. {
  3094. if (!macb_is_gem(bp))
  3095. return;
  3096. gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
  3097. }
  3098. static int macb_set_features(struct net_device *netdev,
  3099. netdev_features_t features)
  3100. {
  3101. struct macb *bp = netdev_priv(netdev);
  3102. netdev_features_t changed = features ^ netdev->features;
  3103. /* TX checksum offload */
  3104. if (changed & NETIF_F_HW_CSUM)
  3105. macb_set_txcsum_feature(bp, features);
  3106. /* RX checksum offload */
  3107. if (changed & NETIF_F_RXCSUM)
  3108. macb_set_rxcsum_feature(bp, features);
  3109. /* RX Flow Filters */
  3110. if (changed & NETIF_F_NTUPLE)
  3111. macb_set_rxflow_feature(bp, features);
  3112. return 0;
  3113. }
  3114. static void macb_restore_features(struct macb *bp)
  3115. {
  3116. struct net_device *netdev = bp->dev;
  3117. netdev_features_t features = netdev->features;
  3118. struct ethtool_rx_fs_item *item;
  3119. /* TX checksum offload */
  3120. macb_set_txcsum_feature(bp, features);
  3121. /* RX checksum offload */
  3122. macb_set_rxcsum_feature(bp, features);
  3123. /* RX Flow Filters */
  3124. list_for_each_entry(item, &bp->rx_fs_list.list, list)
  3125. gem_prog_cmp_regs(bp, &item->fs);
  3126. macb_set_rxflow_feature(bp, features);
  3127. }
  3128. static const struct net_device_ops macb_netdev_ops = {
  3129. .ndo_open = macb_open,
  3130. .ndo_stop = macb_close,
  3131. .ndo_start_xmit = macb_start_xmit,
  3132. .ndo_set_rx_mode = macb_set_rx_mode,
  3133. .ndo_get_stats = macb_get_stats,
  3134. .ndo_eth_ioctl = macb_ioctl,
  3135. .ndo_validate_addr = eth_validate_addr,
  3136. .ndo_change_mtu = macb_change_mtu,
  3137. .ndo_set_mac_address = eth_mac_addr,
  3138. #ifdef CONFIG_NET_POLL_CONTROLLER
  3139. .ndo_poll_controller = macb_poll_controller,
  3140. #endif
  3141. .ndo_set_features = macb_set_features,
  3142. .ndo_features_check = macb_features_check,
  3143. };
  3144. /* Configure peripheral capabilities according to device tree
  3145. * and integration options used
  3146. */
  3147. static void macb_configure_caps(struct macb *bp,
  3148. const struct macb_config *dt_conf)
  3149. {
  3150. u32 dcfg;
  3151. if (dt_conf)
  3152. bp->caps = dt_conf->caps;
  3153. if (hw_is_gem(bp->regs, bp->native_io)) {
  3154. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  3155. dcfg = gem_readl(bp, DCFG1);
  3156. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  3157. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  3158. if (GEM_BFEXT(NO_PCS, dcfg) == 0)
  3159. bp->caps |= MACB_CAPS_PCS;
  3160. dcfg = gem_readl(bp, DCFG12);
  3161. if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
  3162. bp->caps |= MACB_CAPS_HIGH_SPEED;
  3163. dcfg = gem_readl(bp, DCFG2);
  3164. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  3165. bp->caps |= MACB_CAPS_FIFO_MODE;
  3166. #ifdef CONFIG_MACB_USE_HWSTAMP
  3167. if (gem_has_ptp(bp)) {
  3168. if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
  3169. dev_err(&bp->pdev->dev,
  3170. "GEM doesn't support hardware ptp.\n");
  3171. else {
  3172. bp->hw_dma_cap |= HW_DMA_CAP_PTP;
  3173. bp->ptp_info = &gem_ptp_info;
  3174. }
  3175. }
  3176. #endif
  3177. }
  3178. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  3179. }
  3180. static void macb_probe_queues(void __iomem *mem,
  3181. bool native_io,
  3182. unsigned int *queue_mask,
  3183. unsigned int *num_queues)
  3184. {
  3185. *queue_mask = 0x1;
  3186. *num_queues = 1;
  3187. /* is it macb or gem ?
  3188. *
  3189. * We need to read directly from the hardware here because
  3190. * we are early in the probe process and don't have the
  3191. * MACB_CAPS_MACB_IS_GEM flag positioned
  3192. */
  3193. if (!hw_is_gem(mem, native_io))
  3194. return;
  3195. /* bit 0 is never set but queue 0 always exists */
  3196. *queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xff;
  3197. *num_queues = hweight32(*queue_mask);
  3198. }
  3199. static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
  3200. struct clk *rx_clk, struct clk *tsu_clk)
  3201. {
  3202. struct clk_bulk_data clks[] = {
  3203. { .clk = tsu_clk, },
  3204. { .clk = rx_clk, },
  3205. { .clk = pclk, },
  3206. { .clk = hclk, },
  3207. { .clk = tx_clk },
  3208. };
  3209. clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
  3210. }
  3211. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  3212. struct clk **hclk, struct clk **tx_clk,
  3213. struct clk **rx_clk, struct clk **tsu_clk)
  3214. {
  3215. struct macb_platform_data *pdata;
  3216. int err;
  3217. pdata = dev_get_platdata(&pdev->dev);
  3218. if (pdata) {
  3219. *pclk = pdata->pclk;
  3220. *hclk = pdata->hclk;
  3221. } else {
  3222. *pclk = devm_clk_get(&pdev->dev, "pclk");
  3223. *hclk = devm_clk_get(&pdev->dev, "hclk");
  3224. }
  3225. if (IS_ERR_OR_NULL(*pclk))
  3226. return dev_err_probe(&pdev->dev,
  3227. IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
  3228. "failed to get pclk\n");
  3229. if (IS_ERR_OR_NULL(*hclk))
  3230. return dev_err_probe(&pdev->dev,
  3231. IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
  3232. "failed to get hclk\n");
  3233. *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
  3234. if (IS_ERR(*tx_clk))
  3235. return PTR_ERR(*tx_clk);
  3236. *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
  3237. if (IS_ERR(*rx_clk))
  3238. return PTR_ERR(*rx_clk);
  3239. *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
  3240. if (IS_ERR(*tsu_clk))
  3241. return PTR_ERR(*tsu_clk);
  3242. err = clk_prepare_enable(*pclk);
  3243. if (err) {
  3244. dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
  3245. return err;
  3246. }
  3247. err = clk_prepare_enable(*hclk);
  3248. if (err) {
  3249. dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
  3250. goto err_disable_pclk;
  3251. }
  3252. err = clk_prepare_enable(*tx_clk);
  3253. if (err) {
  3254. dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
  3255. goto err_disable_hclk;
  3256. }
  3257. err = clk_prepare_enable(*rx_clk);
  3258. if (err) {
  3259. dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
  3260. goto err_disable_txclk;
  3261. }
  3262. err = clk_prepare_enable(*tsu_clk);
  3263. if (err) {
  3264. dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
  3265. goto err_disable_rxclk;
  3266. }
  3267. return 0;
  3268. err_disable_rxclk:
  3269. clk_disable_unprepare(*rx_clk);
  3270. err_disable_txclk:
  3271. clk_disable_unprepare(*tx_clk);
  3272. err_disable_hclk:
  3273. clk_disable_unprepare(*hclk);
  3274. err_disable_pclk:
  3275. clk_disable_unprepare(*pclk);
  3276. return err;
  3277. }
  3278. static int macb_init(struct platform_device *pdev)
  3279. {
  3280. struct net_device *dev = platform_get_drvdata(pdev);
  3281. unsigned int hw_q, q;
  3282. struct macb *bp = netdev_priv(dev);
  3283. struct macb_queue *queue;
  3284. int err;
  3285. u32 val, reg;
  3286. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  3287. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  3288. /* set the queue register mapping once for all: queue0 has a special
  3289. * register mapping but we don't want to test the queue index then
  3290. * compute the corresponding register offset at run time.
  3291. */
  3292. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  3293. if (!(bp->queue_mask & (1 << hw_q)))
  3294. continue;
  3295. queue = &bp->queues[q];
  3296. queue->bp = bp;
  3297. spin_lock_init(&queue->tx_ptr_lock);
  3298. netif_napi_add(dev, &queue->napi_rx, macb_rx_poll);
  3299. netif_napi_add(dev, &queue->napi_tx, macb_tx_poll);
  3300. if (hw_q) {
  3301. queue->ISR = GEM_ISR(hw_q - 1);
  3302. queue->IER = GEM_IER(hw_q - 1);
  3303. queue->IDR = GEM_IDR(hw_q - 1);
  3304. queue->IMR = GEM_IMR(hw_q - 1);
  3305. queue->TBQP = GEM_TBQP(hw_q - 1);
  3306. queue->RBQP = GEM_RBQP(hw_q - 1);
  3307. queue->RBQS = GEM_RBQS(hw_q - 1);
  3308. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3309. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  3310. queue->TBQPH = GEM_TBQPH(hw_q - 1);
  3311. queue->RBQPH = GEM_RBQPH(hw_q - 1);
  3312. }
  3313. #endif
  3314. } else {
  3315. /* queue0 uses legacy registers */
  3316. queue->ISR = MACB_ISR;
  3317. queue->IER = MACB_IER;
  3318. queue->IDR = MACB_IDR;
  3319. queue->IMR = MACB_IMR;
  3320. queue->TBQP = MACB_TBQP;
  3321. queue->RBQP = MACB_RBQP;
  3322. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3323. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  3324. queue->TBQPH = MACB_TBQPH;
  3325. queue->RBQPH = MACB_RBQPH;
  3326. }
  3327. #endif
  3328. }
  3329. /* get irq: here we use the linux queue index, not the hardware
  3330. * queue index. the queue irq definitions in the device tree
  3331. * must remove the optional gaps that could exist in the
  3332. * hardware queue mask.
  3333. */
  3334. queue->irq = platform_get_irq(pdev, q);
  3335. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  3336. IRQF_SHARED, dev->name, queue);
  3337. if (err) {
  3338. dev_err(&pdev->dev,
  3339. "Unable to request IRQ %d (error %d)\n",
  3340. queue->irq, err);
  3341. return err;
  3342. }
  3343. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  3344. q++;
  3345. }
  3346. dev->netdev_ops = &macb_netdev_ops;
  3347. /* setup appropriated routines according to adapter type */
  3348. if (macb_is_gem(bp)) {
  3349. bp->max_tx_length = GEM_MAX_TX_LEN;
  3350. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  3351. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  3352. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  3353. bp->macbgem_ops.mog_rx = gem_rx;
  3354. dev->ethtool_ops = &gem_ethtool_ops;
  3355. } else {
  3356. bp->max_tx_length = MACB_MAX_TX_LEN;
  3357. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  3358. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  3359. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  3360. bp->macbgem_ops.mog_rx = macb_rx;
  3361. dev->ethtool_ops = &macb_ethtool_ops;
  3362. }
  3363. /* Set features */
  3364. dev->hw_features = NETIF_F_SG;
  3365. /* Check LSO capability */
  3366. if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  3367. dev->hw_features |= MACB_NETIF_LSO;
  3368. /* Checksum offload is only available on gem with packet buffer */
  3369. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  3370. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  3371. if (bp->caps & MACB_CAPS_SG_DISABLED)
  3372. dev->hw_features &= ~NETIF_F_SG;
  3373. dev->features = dev->hw_features;
  3374. /* Check RX Flow Filters support.
  3375. * Max Rx flows set by availability of screeners & compare regs:
  3376. * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
  3377. */
  3378. reg = gem_readl(bp, DCFG8);
  3379. bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
  3380. GEM_BFEXT(T2SCR, reg));
  3381. INIT_LIST_HEAD(&bp->rx_fs_list.list);
  3382. if (bp->max_tuples > 0) {
  3383. /* also needs one ethtype match to check IPv4 */
  3384. if (GEM_BFEXT(SCR2ETH, reg) > 0) {
  3385. /* program this reg now */
  3386. reg = 0;
  3387. reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
  3388. gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
  3389. /* Filtering is supported in hw but don't enable it in kernel now */
  3390. dev->hw_features |= NETIF_F_NTUPLE;
  3391. /* init Rx flow definitions */
  3392. bp->rx_fs_list.count = 0;
  3393. spin_lock_init(&bp->rx_fs_lock);
  3394. } else
  3395. bp->max_tuples = 0;
  3396. }
  3397. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  3398. val = 0;
  3399. if (phy_interface_mode_is_rgmii(bp->phy_interface))
  3400. val = bp->usrio->rgmii;
  3401. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  3402. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  3403. val = bp->usrio->rmii;
  3404. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  3405. val = bp->usrio->mii;
  3406. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  3407. val |= bp->usrio->refclk;
  3408. macb_or_gem_writel(bp, USRIO, val);
  3409. }
  3410. /* Set MII management clock divider */
  3411. val = macb_mdc_clk_div(bp);
  3412. val |= macb_dbw(bp);
  3413. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3414. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  3415. macb_writel(bp, NCFGR, val);
  3416. return 0;
  3417. }
  3418. static const struct macb_usrio_config macb_default_usrio = {
  3419. .mii = MACB_BIT(MII),
  3420. .rmii = MACB_BIT(RMII),
  3421. .rgmii = GEM_BIT(RGMII),
  3422. .refclk = MACB_BIT(CLKEN),
  3423. };
  3424. #if defined(CONFIG_OF)
  3425. /* 1518 rounded up */
  3426. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  3427. /* max number of receive buffers */
  3428. #define AT91ETHER_MAX_RX_DESCR 9
  3429. static struct sifive_fu540_macb_mgmt *mgmt;
  3430. static int at91ether_alloc_coherent(struct macb *lp)
  3431. {
  3432. struct macb_queue *q = &lp->queues[0];
  3433. q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  3434. (AT91ETHER_MAX_RX_DESCR *
  3435. macb_dma_desc_get_size(lp)),
  3436. &q->rx_ring_dma, GFP_KERNEL);
  3437. if (!q->rx_ring)
  3438. return -ENOMEM;
  3439. q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  3440. AT91ETHER_MAX_RX_DESCR *
  3441. AT91ETHER_MAX_RBUFF_SZ,
  3442. &q->rx_buffers_dma, GFP_KERNEL);
  3443. if (!q->rx_buffers) {
  3444. dma_free_coherent(&lp->pdev->dev,
  3445. AT91ETHER_MAX_RX_DESCR *
  3446. macb_dma_desc_get_size(lp),
  3447. q->rx_ring, q->rx_ring_dma);
  3448. q->rx_ring = NULL;
  3449. return -ENOMEM;
  3450. }
  3451. return 0;
  3452. }
  3453. static void at91ether_free_coherent(struct macb *lp)
  3454. {
  3455. struct macb_queue *q = &lp->queues[0];
  3456. if (q->rx_ring) {
  3457. dma_free_coherent(&lp->pdev->dev,
  3458. AT91ETHER_MAX_RX_DESCR *
  3459. macb_dma_desc_get_size(lp),
  3460. q->rx_ring, q->rx_ring_dma);
  3461. q->rx_ring = NULL;
  3462. }
  3463. if (q->rx_buffers) {
  3464. dma_free_coherent(&lp->pdev->dev,
  3465. AT91ETHER_MAX_RX_DESCR *
  3466. AT91ETHER_MAX_RBUFF_SZ,
  3467. q->rx_buffers, q->rx_buffers_dma);
  3468. q->rx_buffers = NULL;
  3469. }
  3470. }
  3471. /* Initialize and start the Receiver and Transmit subsystems */
  3472. static int at91ether_start(struct macb *lp)
  3473. {
  3474. struct macb_queue *q = &lp->queues[0];
  3475. struct macb_dma_desc *desc;
  3476. dma_addr_t addr;
  3477. u32 ctl;
  3478. int i, ret;
  3479. ret = at91ether_alloc_coherent(lp);
  3480. if (ret)
  3481. return ret;
  3482. addr = q->rx_buffers_dma;
  3483. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  3484. desc = macb_rx_desc(q, i);
  3485. macb_set_addr(lp, desc, addr);
  3486. desc->ctrl = 0;
  3487. addr += AT91ETHER_MAX_RBUFF_SZ;
  3488. }
  3489. /* Set the Wrap bit on the last descriptor */
  3490. desc->addr |= MACB_BIT(RX_WRAP);
  3491. /* Reset buffer index */
  3492. q->rx_tail = 0;
  3493. /* Program address of descriptor list in Rx Buffer Queue register */
  3494. macb_writel(lp, RBQP, q->rx_ring_dma);
  3495. /* Enable Receive and Transmit */
  3496. ctl = macb_readl(lp, NCR);
  3497. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  3498. /* Enable MAC interrupts */
  3499. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  3500. MACB_BIT(RXUBR) |
  3501. MACB_BIT(ISR_TUND) |
  3502. MACB_BIT(ISR_RLE) |
  3503. MACB_BIT(TCOMP) |
  3504. MACB_BIT(ISR_ROVR) |
  3505. MACB_BIT(HRESP));
  3506. return 0;
  3507. }
  3508. static void at91ether_stop(struct macb *lp)
  3509. {
  3510. u32 ctl;
  3511. /* Disable MAC interrupts */
  3512. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  3513. MACB_BIT(RXUBR) |
  3514. MACB_BIT(ISR_TUND) |
  3515. MACB_BIT(ISR_RLE) |
  3516. MACB_BIT(TCOMP) |
  3517. MACB_BIT(ISR_ROVR) |
  3518. MACB_BIT(HRESP));
  3519. /* Disable Receiver and Transmitter */
  3520. ctl = macb_readl(lp, NCR);
  3521. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  3522. /* Free resources. */
  3523. at91ether_free_coherent(lp);
  3524. }
  3525. /* Open the ethernet interface */
  3526. static int at91ether_open(struct net_device *dev)
  3527. {
  3528. struct macb *lp = netdev_priv(dev);
  3529. u32 ctl;
  3530. int ret;
  3531. ret = pm_runtime_resume_and_get(&lp->pdev->dev);
  3532. if (ret < 0)
  3533. return ret;
  3534. /* Clear internal statistics */
  3535. ctl = macb_readl(lp, NCR);
  3536. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  3537. macb_set_hwaddr(lp);
  3538. ret = at91ether_start(lp);
  3539. if (ret)
  3540. goto pm_exit;
  3541. ret = macb_phylink_connect(lp);
  3542. if (ret)
  3543. goto stop;
  3544. netif_start_queue(dev);
  3545. return 0;
  3546. stop:
  3547. at91ether_stop(lp);
  3548. pm_exit:
  3549. pm_runtime_put_sync(&lp->pdev->dev);
  3550. return ret;
  3551. }
  3552. /* Close the interface */
  3553. static int at91ether_close(struct net_device *dev)
  3554. {
  3555. struct macb *lp = netdev_priv(dev);
  3556. netif_stop_queue(dev);
  3557. phylink_stop(lp->phylink);
  3558. phylink_disconnect_phy(lp->phylink);
  3559. at91ether_stop(lp);
  3560. return pm_runtime_put(&lp->pdev->dev);
  3561. }
  3562. /* Transmit packet */
  3563. static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
  3564. struct net_device *dev)
  3565. {
  3566. struct macb *lp = netdev_priv(dev);
  3567. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  3568. int desc = 0;
  3569. netif_stop_queue(dev);
  3570. /* Store packet information (to free when Tx completed) */
  3571. lp->rm9200_txq[desc].skb = skb;
  3572. lp->rm9200_txq[desc].size = skb->len;
  3573. lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
  3574. skb->len, DMA_TO_DEVICE);
  3575. if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
  3576. dev_kfree_skb_any(skb);
  3577. dev->stats.tx_dropped++;
  3578. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  3579. return NETDEV_TX_OK;
  3580. }
  3581. /* Set address of the data in the Transmit Address register */
  3582. macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
  3583. /* Set length of the packet in the Transmit Control register */
  3584. macb_writel(lp, TCR, skb->len);
  3585. } else {
  3586. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  3587. return NETDEV_TX_BUSY;
  3588. }
  3589. return NETDEV_TX_OK;
  3590. }
  3591. /* Extract received frame from buffer descriptors and sent to upper layers.
  3592. * (Called from interrupt context)
  3593. */
  3594. static void at91ether_rx(struct net_device *dev)
  3595. {
  3596. struct macb *lp = netdev_priv(dev);
  3597. struct macb_queue *q = &lp->queues[0];
  3598. struct macb_dma_desc *desc;
  3599. unsigned char *p_recv;
  3600. struct sk_buff *skb;
  3601. unsigned int pktlen;
  3602. desc = macb_rx_desc(q, q->rx_tail);
  3603. while (desc->addr & MACB_BIT(RX_USED)) {
  3604. p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  3605. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  3606. skb = netdev_alloc_skb(dev, pktlen + 2);
  3607. if (skb) {
  3608. skb_reserve(skb, 2);
  3609. skb_put_data(skb, p_recv, pktlen);
  3610. skb->protocol = eth_type_trans(skb, dev);
  3611. dev->stats.rx_packets++;
  3612. dev->stats.rx_bytes += pktlen;
  3613. netif_rx(skb);
  3614. } else {
  3615. dev->stats.rx_dropped++;
  3616. }
  3617. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  3618. dev->stats.multicast++;
  3619. /* reset ownership bit */
  3620. desc->addr &= ~MACB_BIT(RX_USED);
  3621. /* wrap after last buffer */
  3622. if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  3623. q->rx_tail = 0;
  3624. else
  3625. q->rx_tail++;
  3626. desc = macb_rx_desc(q, q->rx_tail);
  3627. }
  3628. }
  3629. /* MAC interrupt handler */
  3630. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  3631. {
  3632. struct net_device *dev = dev_id;
  3633. struct macb *lp = netdev_priv(dev);
  3634. u32 intstatus, ctl;
  3635. unsigned int desc;
  3636. /* MAC Interrupt Status register indicates what interrupts are pending.
  3637. * It is automatically cleared once read.
  3638. */
  3639. intstatus = macb_readl(lp, ISR);
  3640. /* Receive complete */
  3641. if (intstatus & MACB_BIT(RCOMP))
  3642. at91ether_rx(dev);
  3643. /* Transmit complete */
  3644. if (intstatus & MACB_BIT(TCOMP)) {
  3645. /* The TCOM bit is set even if the transmission failed */
  3646. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  3647. dev->stats.tx_errors++;
  3648. desc = 0;
  3649. if (lp->rm9200_txq[desc].skb) {
  3650. dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
  3651. lp->rm9200_txq[desc].skb = NULL;
  3652. dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
  3653. lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
  3654. dev->stats.tx_packets++;
  3655. dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
  3656. }
  3657. netif_wake_queue(dev);
  3658. }
  3659. /* Work-around for EMAC Errata section 41.3.1 */
  3660. if (intstatus & MACB_BIT(RXUBR)) {
  3661. ctl = macb_readl(lp, NCR);
  3662. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  3663. wmb();
  3664. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  3665. }
  3666. if (intstatus & MACB_BIT(ISR_ROVR))
  3667. netdev_err(dev, "ROVR error\n");
  3668. return IRQ_HANDLED;
  3669. }
  3670. #ifdef CONFIG_NET_POLL_CONTROLLER
  3671. static void at91ether_poll_controller(struct net_device *dev)
  3672. {
  3673. unsigned long flags;
  3674. local_irq_save(flags);
  3675. at91ether_interrupt(dev->irq, dev);
  3676. local_irq_restore(flags);
  3677. }
  3678. #endif
  3679. static const struct net_device_ops at91ether_netdev_ops = {
  3680. .ndo_open = at91ether_open,
  3681. .ndo_stop = at91ether_close,
  3682. .ndo_start_xmit = at91ether_start_xmit,
  3683. .ndo_get_stats = macb_get_stats,
  3684. .ndo_set_rx_mode = macb_set_rx_mode,
  3685. .ndo_set_mac_address = eth_mac_addr,
  3686. .ndo_eth_ioctl = macb_ioctl,
  3687. .ndo_validate_addr = eth_validate_addr,
  3688. #ifdef CONFIG_NET_POLL_CONTROLLER
  3689. .ndo_poll_controller = at91ether_poll_controller,
  3690. #endif
  3691. };
  3692. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  3693. struct clk **hclk, struct clk **tx_clk,
  3694. struct clk **rx_clk, struct clk **tsu_clk)
  3695. {
  3696. int err;
  3697. *hclk = NULL;
  3698. *tx_clk = NULL;
  3699. *rx_clk = NULL;
  3700. *tsu_clk = NULL;
  3701. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  3702. if (IS_ERR(*pclk))
  3703. return PTR_ERR(*pclk);
  3704. err = clk_prepare_enable(*pclk);
  3705. if (err) {
  3706. dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
  3707. return err;
  3708. }
  3709. return 0;
  3710. }
  3711. static int at91ether_init(struct platform_device *pdev)
  3712. {
  3713. struct net_device *dev = platform_get_drvdata(pdev);
  3714. struct macb *bp = netdev_priv(dev);
  3715. int err;
  3716. bp->queues[0].bp = bp;
  3717. dev->netdev_ops = &at91ether_netdev_ops;
  3718. dev->ethtool_ops = &macb_ethtool_ops;
  3719. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  3720. 0, dev->name, dev);
  3721. if (err)
  3722. return err;
  3723. macb_writel(bp, NCR, 0);
  3724. macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
  3725. return 0;
  3726. }
  3727. static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
  3728. unsigned long parent_rate)
  3729. {
  3730. return mgmt->rate;
  3731. }
  3732. static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
  3733. unsigned long *parent_rate)
  3734. {
  3735. if (WARN_ON(rate < 2500000))
  3736. return 2500000;
  3737. else if (rate == 2500000)
  3738. return 2500000;
  3739. else if (WARN_ON(rate < 13750000))
  3740. return 2500000;
  3741. else if (WARN_ON(rate < 25000000))
  3742. return 25000000;
  3743. else if (rate == 25000000)
  3744. return 25000000;
  3745. else if (WARN_ON(rate < 75000000))
  3746. return 25000000;
  3747. else if (WARN_ON(rate < 125000000))
  3748. return 125000000;
  3749. else if (rate == 125000000)
  3750. return 125000000;
  3751. WARN_ON(rate > 125000000);
  3752. return 125000000;
  3753. }
  3754. static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
  3755. unsigned long parent_rate)
  3756. {
  3757. rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
  3758. if (rate != 125000000)
  3759. iowrite32(1, mgmt->reg);
  3760. else
  3761. iowrite32(0, mgmt->reg);
  3762. mgmt->rate = rate;
  3763. return 0;
  3764. }
  3765. static const struct clk_ops fu540_c000_ops = {
  3766. .recalc_rate = fu540_macb_tx_recalc_rate,
  3767. .round_rate = fu540_macb_tx_round_rate,
  3768. .set_rate = fu540_macb_tx_set_rate,
  3769. };
  3770. static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
  3771. struct clk **hclk, struct clk **tx_clk,
  3772. struct clk **rx_clk, struct clk **tsu_clk)
  3773. {
  3774. struct clk_init_data init;
  3775. int err = 0;
  3776. err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
  3777. if (err)
  3778. return err;
  3779. mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
  3780. if (!mgmt) {
  3781. err = -ENOMEM;
  3782. goto err_disable_clks;
  3783. }
  3784. init.name = "sifive-gemgxl-mgmt";
  3785. init.ops = &fu540_c000_ops;
  3786. init.flags = 0;
  3787. init.num_parents = 0;
  3788. mgmt->rate = 0;
  3789. mgmt->hw.init = &init;
  3790. *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
  3791. if (IS_ERR(*tx_clk)) {
  3792. err = PTR_ERR(*tx_clk);
  3793. goto err_disable_clks;
  3794. }
  3795. err = clk_prepare_enable(*tx_clk);
  3796. if (err) {
  3797. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  3798. *tx_clk = NULL;
  3799. goto err_disable_clks;
  3800. } else {
  3801. dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
  3802. }
  3803. return 0;
  3804. err_disable_clks:
  3805. macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
  3806. return err;
  3807. }
  3808. static int fu540_c000_init(struct platform_device *pdev)
  3809. {
  3810. mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
  3811. if (IS_ERR(mgmt->reg))
  3812. return PTR_ERR(mgmt->reg);
  3813. return macb_init(pdev);
  3814. }
  3815. static int init_reset_optional(struct platform_device *pdev)
  3816. {
  3817. struct net_device *dev = platform_get_drvdata(pdev);
  3818. struct macb *bp = netdev_priv(dev);
  3819. int ret;
  3820. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3821. /* Ensure PHY device used in SGMII mode is ready */
  3822. bp->sgmii_phy = devm_phy_optional_get(&pdev->dev, NULL);
  3823. if (IS_ERR(bp->sgmii_phy))
  3824. return dev_err_probe(&pdev->dev, PTR_ERR(bp->sgmii_phy),
  3825. "failed to get SGMII PHY\n");
  3826. ret = phy_init(bp->sgmii_phy);
  3827. if (ret)
  3828. return dev_err_probe(&pdev->dev, ret,
  3829. "failed to init SGMII PHY\n");
  3830. ret = zynqmp_pm_is_function_supported(PM_IOCTL, IOCTL_SET_GEM_CONFIG);
  3831. if (!ret) {
  3832. u32 pm_info[2];
  3833. ret = of_property_read_u32_array(pdev->dev.of_node, "power-domains",
  3834. pm_info, ARRAY_SIZE(pm_info));
  3835. if (ret) {
  3836. dev_err(&pdev->dev, "Failed to read power management information\n");
  3837. goto err_out_phy_exit;
  3838. }
  3839. ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_FIXED, 0);
  3840. if (ret)
  3841. goto err_out_phy_exit;
  3842. ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_SGMII_MODE, 1);
  3843. if (ret)
  3844. goto err_out_phy_exit;
  3845. }
  3846. }
  3847. /* Fully reset controller at hardware level if mapped in device tree */
  3848. ret = device_reset_optional(&pdev->dev);
  3849. if (ret) {
  3850. phy_exit(bp->sgmii_phy);
  3851. return dev_err_probe(&pdev->dev, ret, "failed to reset controller");
  3852. }
  3853. ret = macb_init(pdev);
  3854. err_out_phy_exit:
  3855. if (ret)
  3856. phy_exit(bp->sgmii_phy);
  3857. return ret;
  3858. }
  3859. static const struct macb_usrio_config sama7g5_usrio = {
  3860. .mii = 0,
  3861. .rmii = 1,
  3862. .rgmii = 2,
  3863. .refclk = BIT(2),
  3864. .hdfctlen = BIT(6),
  3865. };
  3866. static const struct macb_config fu540_c000_config = {
  3867. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
  3868. MACB_CAPS_GEM_HAS_PTP,
  3869. .dma_burst_length = 16,
  3870. .clk_init = fu540_c000_clk_init,
  3871. .init = fu540_c000_init,
  3872. .jumbo_max_len = 10240,
  3873. .usrio = &macb_default_usrio,
  3874. };
  3875. static const struct macb_config at91sam9260_config = {
  3876. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3877. .clk_init = macb_clk_init,
  3878. .init = macb_init,
  3879. .usrio = &macb_default_usrio,
  3880. };
  3881. static const struct macb_config sama5d3macb_config = {
  3882. .caps = MACB_CAPS_SG_DISABLED |
  3883. MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3884. .clk_init = macb_clk_init,
  3885. .init = macb_init,
  3886. .usrio = &macb_default_usrio,
  3887. };
  3888. static const struct macb_config pc302gem_config = {
  3889. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  3890. .dma_burst_length = 16,
  3891. .clk_init = macb_clk_init,
  3892. .init = macb_init,
  3893. .usrio = &macb_default_usrio,
  3894. };
  3895. static const struct macb_config sama5d2_config = {
  3896. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3897. .dma_burst_length = 16,
  3898. .clk_init = macb_clk_init,
  3899. .init = macb_init,
  3900. .usrio = &macb_default_usrio,
  3901. };
  3902. static const struct macb_config sama5d29_config = {
  3903. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP,
  3904. .dma_burst_length = 16,
  3905. .clk_init = macb_clk_init,
  3906. .init = macb_init,
  3907. .usrio = &macb_default_usrio,
  3908. };
  3909. static const struct macb_config sama5d3_config = {
  3910. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3911. MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  3912. .dma_burst_length = 16,
  3913. .clk_init = macb_clk_init,
  3914. .init = macb_init,
  3915. .jumbo_max_len = 10240,
  3916. .usrio = &macb_default_usrio,
  3917. };
  3918. static const struct macb_config sama5d4_config = {
  3919. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3920. .dma_burst_length = 4,
  3921. .clk_init = macb_clk_init,
  3922. .init = macb_init,
  3923. .usrio = &macb_default_usrio,
  3924. };
  3925. static const struct macb_config emac_config = {
  3926. .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
  3927. .clk_init = at91ether_clk_init,
  3928. .init = at91ether_init,
  3929. .usrio = &macb_default_usrio,
  3930. };
  3931. static const struct macb_config np4_config = {
  3932. .caps = MACB_CAPS_USRIO_DISABLED,
  3933. .clk_init = macb_clk_init,
  3934. .init = macb_init,
  3935. .usrio = &macb_default_usrio,
  3936. };
  3937. static const struct macb_config zynqmp_config = {
  3938. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3939. MACB_CAPS_JUMBO |
  3940. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
  3941. .dma_burst_length = 16,
  3942. .clk_init = macb_clk_init,
  3943. .init = init_reset_optional,
  3944. .jumbo_max_len = 10240,
  3945. .usrio = &macb_default_usrio,
  3946. };
  3947. static const struct macb_config zynq_config = {
  3948. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
  3949. MACB_CAPS_NEEDS_RSTONUBR,
  3950. .dma_burst_length = 16,
  3951. .clk_init = macb_clk_init,
  3952. .init = macb_init,
  3953. .usrio = &macb_default_usrio,
  3954. };
  3955. static const struct macb_config mpfs_config = {
  3956. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3957. MACB_CAPS_JUMBO |
  3958. MACB_CAPS_GEM_HAS_PTP,
  3959. .dma_burst_length = 16,
  3960. .clk_init = macb_clk_init,
  3961. .init = init_reset_optional,
  3962. .usrio = &macb_default_usrio,
  3963. .jumbo_max_len = 10240,
  3964. };
  3965. static const struct macb_config sama7g5_gem_config = {
  3966. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
  3967. MACB_CAPS_MIIONRGMII,
  3968. .dma_burst_length = 16,
  3969. .clk_init = macb_clk_init,
  3970. .init = macb_init,
  3971. .usrio = &sama7g5_usrio,
  3972. };
  3973. static const struct macb_config sama7g5_emac_config = {
  3974. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
  3975. MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII,
  3976. .dma_burst_length = 16,
  3977. .clk_init = macb_clk_init,
  3978. .init = macb_init,
  3979. .usrio = &sama7g5_usrio,
  3980. };
  3981. static const struct macb_config versal_config = {
  3982. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
  3983. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH | MACB_CAPS_NEED_TSUCLK,
  3984. .dma_burst_length = 16,
  3985. .clk_init = macb_clk_init,
  3986. .init = init_reset_optional,
  3987. .jumbo_max_len = 10240,
  3988. .usrio = &macb_default_usrio,
  3989. };
  3990. static const struct of_device_id macb_dt_ids[] = {
  3991. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  3992. { .compatible = "cdns,macb" },
  3993. { .compatible = "cdns,np4-macb", .data = &np4_config },
  3994. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  3995. { .compatible = "cdns,gem", .data = &pc302gem_config },
  3996. { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
  3997. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  3998. { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
  3999. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  4000. { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
  4001. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  4002. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  4003. { .compatible = "cdns,emac", .data = &emac_config },
  4004. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, /* deprecated */
  4005. { .compatible = "cdns,zynq-gem", .data = &zynq_config }, /* deprecated */
  4006. { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
  4007. { .compatible = "microchip,mpfs-macb", .data = &mpfs_config },
  4008. { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
  4009. { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
  4010. { .compatible = "xlnx,zynqmp-gem", .data = &zynqmp_config},
  4011. { .compatible = "xlnx,zynq-gem", .data = &zynq_config },
  4012. { .compatible = "xlnx,versal-gem", .data = &versal_config},
  4013. { /* sentinel */ }
  4014. };
  4015. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  4016. #endif /* CONFIG_OF */
  4017. static const struct macb_config default_gem_config = {
  4018. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  4019. MACB_CAPS_JUMBO |
  4020. MACB_CAPS_GEM_HAS_PTP,
  4021. .dma_burst_length = 16,
  4022. .clk_init = macb_clk_init,
  4023. .init = macb_init,
  4024. .usrio = &macb_default_usrio,
  4025. .jumbo_max_len = 10240,
  4026. };
  4027. static int macb_probe(struct platform_device *pdev)
  4028. {
  4029. const struct macb_config *macb_config = &default_gem_config;
  4030. int (*clk_init)(struct platform_device *, struct clk **,
  4031. struct clk **, struct clk **, struct clk **,
  4032. struct clk **) = macb_config->clk_init;
  4033. int (*init)(struct platform_device *) = macb_config->init;
  4034. struct device_node *np = pdev->dev.of_node;
  4035. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  4036. struct clk *tsu_clk = NULL;
  4037. unsigned int queue_mask, num_queues;
  4038. bool native_io;
  4039. phy_interface_t interface;
  4040. struct net_device *dev;
  4041. struct resource *regs;
  4042. void __iomem *mem;
  4043. struct macb *bp;
  4044. int err, val;
  4045. mem = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
  4046. if (IS_ERR(mem))
  4047. return PTR_ERR(mem);
  4048. if (np) {
  4049. const struct of_device_id *match;
  4050. match = of_match_node(macb_dt_ids, np);
  4051. if (match && match->data) {
  4052. macb_config = match->data;
  4053. clk_init = macb_config->clk_init;
  4054. init = macb_config->init;
  4055. }
  4056. }
  4057. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
  4058. if (err)
  4059. return err;
  4060. pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
  4061. pm_runtime_use_autosuspend(&pdev->dev);
  4062. pm_runtime_get_noresume(&pdev->dev);
  4063. pm_runtime_set_active(&pdev->dev);
  4064. pm_runtime_enable(&pdev->dev);
  4065. native_io = hw_is_native_io(mem);
  4066. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  4067. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  4068. if (!dev) {
  4069. err = -ENOMEM;
  4070. goto err_disable_clocks;
  4071. }
  4072. dev->base_addr = regs->start;
  4073. SET_NETDEV_DEV(dev, &pdev->dev);
  4074. bp = netdev_priv(dev);
  4075. bp->pdev = pdev;
  4076. bp->dev = dev;
  4077. bp->regs = mem;
  4078. bp->native_io = native_io;
  4079. if (native_io) {
  4080. bp->macb_reg_readl = hw_readl_native;
  4081. bp->macb_reg_writel = hw_writel_native;
  4082. } else {
  4083. bp->macb_reg_readl = hw_readl;
  4084. bp->macb_reg_writel = hw_writel;
  4085. }
  4086. bp->num_queues = num_queues;
  4087. bp->queue_mask = queue_mask;
  4088. if (macb_config)
  4089. bp->dma_burst_length = macb_config->dma_burst_length;
  4090. bp->pclk = pclk;
  4091. bp->hclk = hclk;
  4092. bp->tx_clk = tx_clk;
  4093. bp->rx_clk = rx_clk;
  4094. bp->tsu_clk = tsu_clk;
  4095. if (macb_config)
  4096. bp->jumbo_max_len = macb_config->jumbo_max_len;
  4097. bp->wol = 0;
  4098. if (of_get_property(np, "magic-packet", NULL))
  4099. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  4100. device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  4101. bp->usrio = macb_config->usrio;
  4102. spin_lock_init(&bp->lock);
  4103. /* setup capabilities */
  4104. macb_configure_caps(bp, macb_config);
  4105. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  4106. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  4107. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
  4108. bp->hw_dma_cap |= HW_DMA_CAP_64B;
  4109. }
  4110. #endif
  4111. platform_set_drvdata(pdev, dev);
  4112. dev->irq = platform_get_irq(pdev, 0);
  4113. if (dev->irq < 0) {
  4114. err = dev->irq;
  4115. goto err_out_free_netdev;
  4116. }
  4117. /* MTU range: 68 - 1500 or 10240 */
  4118. dev->min_mtu = GEM_MTU_MIN_SIZE;
  4119. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  4120. dev->max_mtu = bp->jumbo_max_len - ETH_HLEN - ETH_FCS_LEN;
  4121. else
  4122. dev->max_mtu = ETH_DATA_LEN;
  4123. if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
  4124. val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
  4125. if (val)
  4126. bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
  4127. macb_dma_desc_get_size(bp);
  4128. val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
  4129. if (val)
  4130. bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
  4131. macb_dma_desc_get_size(bp);
  4132. }
  4133. bp->rx_intr_mask = MACB_RX_INT_FLAGS;
  4134. if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
  4135. bp->rx_intr_mask |= MACB_BIT(RXUBR);
  4136. err = of_get_ethdev_address(np, bp->dev);
  4137. if (err == -EPROBE_DEFER)
  4138. goto err_out_free_netdev;
  4139. else if (err)
  4140. macb_get_hwaddr(bp);
  4141. err = of_get_phy_mode(np, &interface);
  4142. if (err)
  4143. /* not found in DT, MII by default */
  4144. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  4145. else
  4146. bp->phy_interface = interface;
  4147. /* IP specific init */
  4148. err = init(pdev);
  4149. if (err)
  4150. goto err_out_free_netdev;
  4151. err = macb_mii_init(bp);
  4152. if (err)
  4153. goto err_out_phy_exit;
  4154. netif_carrier_off(dev);
  4155. err = register_netdev(dev);
  4156. if (err) {
  4157. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  4158. goto err_out_unregister_mdio;
  4159. }
  4160. tasklet_setup(&bp->hresp_err_tasklet, macb_hresp_error_task);
  4161. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  4162. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  4163. dev->base_addr, dev->irq, dev->dev_addr);
  4164. pm_runtime_mark_last_busy(&bp->pdev->dev);
  4165. pm_runtime_put_autosuspend(&bp->pdev->dev);
  4166. return 0;
  4167. err_out_unregister_mdio:
  4168. mdiobus_unregister(bp->mii_bus);
  4169. mdiobus_free(bp->mii_bus);
  4170. err_out_phy_exit:
  4171. phy_exit(bp->sgmii_phy);
  4172. err_out_free_netdev:
  4173. free_netdev(dev);
  4174. err_disable_clocks:
  4175. macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
  4176. pm_runtime_disable(&pdev->dev);
  4177. pm_runtime_set_suspended(&pdev->dev);
  4178. pm_runtime_dont_use_autosuspend(&pdev->dev);
  4179. return err;
  4180. }
  4181. static int macb_remove(struct platform_device *pdev)
  4182. {
  4183. struct net_device *dev;
  4184. struct macb *bp;
  4185. dev = platform_get_drvdata(pdev);
  4186. if (dev) {
  4187. bp = netdev_priv(dev);
  4188. phy_exit(bp->sgmii_phy);
  4189. mdiobus_unregister(bp->mii_bus);
  4190. mdiobus_free(bp->mii_bus);
  4191. unregister_netdev(dev);
  4192. tasklet_kill(&bp->hresp_err_tasklet);
  4193. pm_runtime_disable(&pdev->dev);
  4194. pm_runtime_dont_use_autosuspend(&pdev->dev);
  4195. if (!pm_runtime_suspended(&pdev->dev)) {
  4196. macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk,
  4197. bp->rx_clk, bp->tsu_clk);
  4198. pm_runtime_set_suspended(&pdev->dev);
  4199. }
  4200. phylink_destroy(bp->phylink);
  4201. free_netdev(dev);
  4202. }
  4203. return 0;
  4204. }
  4205. static int __maybe_unused macb_suspend(struct device *dev)
  4206. {
  4207. struct net_device *netdev = dev_get_drvdata(dev);
  4208. struct macb *bp = netdev_priv(netdev);
  4209. struct macb_queue *queue;
  4210. unsigned long flags;
  4211. unsigned int q;
  4212. int err;
  4213. if (!device_may_wakeup(&bp->dev->dev))
  4214. phy_exit(bp->sgmii_phy);
  4215. if (!netif_running(netdev))
  4216. return 0;
  4217. if (bp->wol & MACB_WOL_ENABLED) {
  4218. spin_lock_irqsave(&bp->lock, flags);
  4219. /* Flush all status bits */
  4220. macb_writel(bp, TSR, -1);
  4221. macb_writel(bp, RSR, -1);
  4222. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4223. ++q, ++queue) {
  4224. /* Disable all interrupts */
  4225. queue_writel(queue, IDR, -1);
  4226. queue_readl(queue, ISR);
  4227. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  4228. queue_writel(queue, ISR, -1);
  4229. }
  4230. /* Change interrupt handler and
  4231. * Enable WoL IRQ on queue 0
  4232. */
  4233. devm_free_irq(dev, bp->queues[0].irq, bp->queues);
  4234. if (macb_is_gem(bp)) {
  4235. err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
  4236. IRQF_SHARED, netdev->name, bp->queues);
  4237. if (err) {
  4238. dev_err(dev,
  4239. "Unable to request IRQ %d (error %d)\n",
  4240. bp->queues[0].irq, err);
  4241. spin_unlock_irqrestore(&bp->lock, flags);
  4242. return err;
  4243. }
  4244. queue_writel(bp->queues, IER, GEM_BIT(WOL));
  4245. gem_writel(bp, WOL, MACB_BIT(MAG));
  4246. } else {
  4247. err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
  4248. IRQF_SHARED, netdev->name, bp->queues);
  4249. if (err) {
  4250. dev_err(dev,
  4251. "Unable to request IRQ %d (error %d)\n",
  4252. bp->queues[0].irq, err);
  4253. spin_unlock_irqrestore(&bp->lock, flags);
  4254. return err;
  4255. }
  4256. queue_writel(bp->queues, IER, MACB_BIT(WOL));
  4257. macb_writel(bp, WOL, MACB_BIT(MAG));
  4258. }
  4259. spin_unlock_irqrestore(&bp->lock, flags);
  4260. enable_irq_wake(bp->queues[0].irq);
  4261. }
  4262. netif_device_detach(netdev);
  4263. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4264. ++q, ++queue) {
  4265. napi_disable(&queue->napi_rx);
  4266. napi_disable(&queue->napi_tx);
  4267. }
  4268. if (!(bp->wol & MACB_WOL_ENABLED)) {
  4269. rtnl_lock();
  4270. phylink_stop(bp->phylink);
  4271. rtnl_unlock();
  4272. spin_lock_irqsave(&bp->lock, flags);
  4273. macb_reset_hw(bp);
  4274. spin_unlock_irqrestore(&bp->lock, flags);
  4275. }
  4276. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  4277. bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
  4278. if (netdev->hw_features & NETIF_F_NTUPLE)
  4279. bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
  4280. if (bp->ptp_info)
  4281. bp->ptp_info->ptp_remove(netdev);
  4282. if (!device_may_wakeup(dev))
  4283. pm_runtime_force_suspend(dev);
  4284. return 0;
  4285. }
  4286. static int __maybe_unused macb_resume(struct device *dev)
  4287. {
  4288. struct net_device *netdev = dev_get_drvdata(dev);
  4289. struct macb *bp = netdev_priv(netdev);
  4290. struct macb_queue *queue;
  4291. unsigned long flags;
  4292. unsigned int q;
  4293. int err;
  4294. if (!device_may_wakeup(&bp->dev->dev))
  4295. phy_init(bp->sgmii_phy);
  4296. if (!netif_running(netdev))
  4297. return 0;
  4298. if (!device_may_wakeup(dev))
  4299. pm_runtime_force_resume(dev);
  4300. if (bp->wol & MACB_WOL_ENABLED) {
  4301. spin_lock_irqsave(&bp->lock, flags);
  4302. /* Disable WoL */
  4303. if (macb_is_gem(bp)) {
  4304. queue_writel(bp->queues, IDR, GEM_BIT(WOL));
  4305. gem_writel(bp, WOL, 0);
  4306. } else {
  4307. queue_writel(bp->queues, IDR, MACB_BIT(WOL));
  4308. macb_writel(bp, WOL, 0);
  4309. }
  4310. /* Clear ISR on queue 0 */
  4311. queue_readl(bp->queues, ISR);
  4312. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  4313. queue_writel(bp->queues, ISR, -1);
  4314. /* Replace interrupt handler on queue 0 */
  4315. devm_free_irq(dev, bp->queues[0].irq, bp->queues);
  4316. err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
  4317. IRQF_SHARED, netdev->name, bp->queues);
  4318. if (err) {
  4319. dev_err(dev,
  4320. "Unable to request IRQ %d (error %d)\n",
  4321. bp->queues[0].irq, err);
  4322. spin_unlock_irqrestore(&bp->lock, flags);
  4323. return err;
  4324. }
  4325. spin_unlock_irqrestore(&bp->lock, flags);
  4326. disable_irq_wake(bp->queues[0].irq);
  4327. /* Now make sure we disable phy before moving
  4328. * to common restore path
  4329. */
  4330. rtnl_lock();
  4331. phylink_stop(bp->phylink);
  4332. rtnl_unlock();
  4333. }
  4334. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4335. ++q, ++queue) {
  4336. napi_enable(&queue->napi_rx);
  4337. napi_enable(&queue->napi_tx);
  4338. }
  4339. if (netdev->hw_features & NETIF_F_NTUPLE)
  4340. gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
  4341. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  4342. macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
  4343. macb_writel(bp, NCR, MACB_BIT(MPE));
  4344. macb_init_hw(bp);
  4345. macb_set_rx_mode(netdev);
  4346. macb_restore_features(bp);
  4347. rtnl_lock();
  4348. phylink_start(bp->phylink);
  4349. rtnl_unlock();
  4350. netif_device_attach(netdev);
  4351. if (bp->ptp_info)
  4352. bp->ptp_info->ptp_init(netdev);
  4353. return 0;
  4354. }
  4355. static int __maybe_unused macb_runtime_suspend(struct device *dev)
  4356. {
  4357. struct net_device *netdev = dev_get_drvdata(dev);
  4358. struct macb *bp = netdev_priv(netdev);
  4359. if (!(device_may_wakeup(dev)))
  4360. macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
  4361. else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK))
  4362. macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
  4363. return 0;
  4364. }
  4365. static int __maybe_unused macb_runtime_resume(struct device *dev)
  4366. {
  4367. struct net_device *netdev = dev_get_drvdata(dev);
  4368. struct macb *bp = netdev_priv(netdev);
  4369. if (!(device_may_wakeup(dev))) {
  4370. clk_prepare_enable(bp->pclk);
  4371. clk_prepare_enable(bp->hclk);
  4372. clk_prepare_enable(bp->tx_clk);
  4373. clk_prepare_enable(bp->rx_clk);
  4374. clk_prepare_enable(bp->tsu_clk);
  4375. } else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK)) {
  4376. clk_prepare_enable(bp->tsu_clk);
  4377. }
  4378. return 0;
  4379. }
  4380. static const struct dev_pm_ops macb_pm_ops = {
  4381. SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
  4382. SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
  4383. };
  4384. static struct platform_driver macb_driver = {
  4385. .probe = macb_probe,
  4386. .remove = macb_remove,
  4387. .driver = {
  4388. .name = "macb",
  4389. .of_match_table = of_match_ptr(macb_dt_ids),
  4390. .pm = &macb_pm_ops,
  4391. },
  4392. };
  4393. module_platform_driver(macb_driver);
  4394. MODULE_LICENSE("GPL");
  4395. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  4396. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  4397. MODULE_ALIAS("platform:macb");