macb_main.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113
  1. /*
  2. * Cadence MACB/GEM Ethernet Controller driver
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/circ_buf.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/platform_data/macb.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/phy.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_mdio.h>
  33. #include <linux/of_net.h>
  34. #include <linux/ip.h>
  35. #include <linux/udp.h>
  36. #include <linux/tcp.h>
  37. #include "macb.h"
  38. #define MACB_RX_BUFFER_SIZE 128
  39. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  40. #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
  41. #define MIN_RX_RING_SIZE 64
  42. #define MAX_RX_RING_SIZE 8192
  43. #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  44. * (bp)->rx_ring_size)
  45. #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
  46. #define MIN_TX_RING_SIZE 64
  47. #define MAX_TX_RING_SIZE 4096
  48. #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
  49. * (bp)->tx_ring_size)
  50. /* level of occupied TX descriptors under which we wake up TX process */
  51. #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
  52. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  53. | MACB_BIT(ISR_ROVR))
  54. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  55. | MACB_BIT(ISR_RLE) \
  56. | MACB_BIT(TXERR))
  57. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
  58. /* Max length of transmit frame must be a multiple of 8 bytes */
  59. #define MACB_TX_LEN_ALIGN 8
  60. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  61. #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  62. #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
  63. #define MACB_NETIF_LSO NETIF_F_TSO
  64. #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  65. #define MACB_WOL_ENABLED (0x1 << 1)
  66. /* Graceful stop timeouts in us. We should allow up to
  67. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  68. */
  69. #define MACB_HALT_TIMEOUT 1230
  70. /* DMA buffer descriptor might be different size
  71. * depends on hardware configuration:
  72. *
  73. * 1. dma address width 32 bits:
  74. * word 1: 32 bit address of Data Buffer
  75. * word 2: control
  76. *
  77. * 2. dma address width 64 bits:
  78. * word 1: 32 bit address of Data Buffer
  79. * word 2: control
  80. * word 3: upper 32 bit address of Data Buffer
  81. * word 4: unused
  82. *
  83. * 3. dma address width 32 bits with hardware timestamping:
  84. * word 1: 32 bit address of Data Buffer
  85. * word 2: control
  86. * word 3: timestamp word 1
  87. * word 4: timestamp word 2
  88. *
  89. * 4. dma address width 64 bits with hardware timestamping:
  90. * word 1: 32 bit address of Data Buffer
  91. * word 2: control
  92. * word 3: upper 32 bit address of Data Buffer
  93. * word 4: unused
  94. * word 5: timestamp word 1
  95. * word 6: timestamp word 2
  96. */
  97. static unsigned int macb_dma_desc_get_size(struct macb *bp)
  98. {
  99. #ifdef MACB_EXT_DESC
  100. unsigned int desc_size;
  101. switch (bp->hw_dma_cap) {
  102. case HW_DMA_CAP_64B:
  103. desc_size = sizeof(struct macb_dma_desc)
  104. + sizeof(struct macb_dma_desc_64);
  105. break;
  106. case HW_DMA_CAP_PTP:
  107. desc_size = sizeof(struct macb_dma_desc)
  108. + sizeof(struct macb_dma_desc_ptp);
  109. break;
  110. case HW_DMA_CAP_64B_PTP:
  111. desc_size = sizeof(struct macb_dma_desc)
  112. + sizeof(struct macb_dma_desc_64)
  113. + sizeof(struct macb_dma_desc_ptp);
  114. break;
  115. default:
  116. desc_size = sizeof(struct macb_dma_desc);
  117. }
  118. return desc_size;
  119. #endif
  120. return sizeof(struct macb_dma_desc);
  121. }
  122. static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
  123. {
  124. #ifdef MACB_EXT_DESC
  125. switch (bp->hw_dma_cap) {
  126. case HW_DMA_CAP_64B:
  127. case HW_DMA_CAP_PTP:
  128. desc_idx <<= 1;
  129. break;
  130. case HW_DMA_CAP_64B_PTP:
  131. desc_idx *= 3;
  132. break;
  133. default:
  134. break;
  135. }
  136. #endif
  137. return desc_idx;
  138. }
  139. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  140. static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
  141. {
  142. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  143. return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
  144. return NULL;
  145. }
  146. #endif
  147. /* Ring buffer accessors */
  148. static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
  149. {
  150. return index & (bp->tx_ring_size - 1);
  151. }
  152. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  153. unsigned int index)
  154. {
  155. index = macb_tx_ring_wrap(queue->bp, index);
  156. index = macb_adj_dma_desc_idx(queue->bp, index);
  157. return &queue->tx_ring[index];
  158. }
  159. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  160. unsigned int index)
  161. {
  162. return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
  163. }
  164. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  165. {
  166. dma_addr_t offset;
  167. offset = macb_tx_ring_wrap(queue->bp, index) *
  168. macb_dma_desc_get_size(queue->bp);
  169. return queue->tx_ring_dma + offset;
  170. }
  171. static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
  172. {
  173. return index & (bp->rx_ring_size - 1);
  174. }
  175. static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
  176. {
  177. index = macb_rx_ring_wrap(queue->bp, index);
  178. index = macb_adj_dma_desc_idx(queue->bp, index);
  179. return &queue->rx_ring[index];
  180. }
  181. static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
  182. {
  183. return queue->rx_buffers + queue->bp->rx_buffer_size *
  184. macb_rx_ring_wrap(queue->bp, index);
  185. }
  186. /* I/O accessors */
  187. static u32 hw_readl_native(struct macb *bp, int offset)
  188. {
  189. return __raw_readl(bp->regs + offset);
  190. }
  191. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  192. {
  193. __raw_writel(value, bp->regs + offset);
  194. }
  195. static u32 hw_readl(struct macb *bp, int offset)
  196. {
  197. return readl_relaxed(bp->regs + offset);
  198. }
  199. static void hw_writel(struct macb *bp, int offset, u32 value)
  200. {
  201. writel_relaxed(value, bp->regs + offset);
  202. }
  203. /* Find the CPU endianness by using the loopback bit of NCR register. When the
  204. * CPU is in big endian we need to program swapped mode for management
  205. * descriptor access.
  206. */
  207. static bool hw_is_native_io(void __iomem *addr)
  208. {
  209. u32 value = MACB_BIT(LLB);
  210. __raw_writel(value, addr + MACB_NCR);
  211. value = __raw_readl(addr + MACB_NCR);
  212. /* Write 0 back to disable everything */
  213. __raw_writel(0, addr + MACB_NCR);
  214. return value == MACB_BIT(LLB);
  215. }
  216. static bool hw_is_gem(void __iomem *addr, bool native_io)
  217. {
  218. u32 id;
  219. if (native_io)
  220. id = __raw_readl(addr + MACB_MID);
  221. else
  222. id = readl_relaxed(addr + MACB_MID);
  223. return MACB_BFEXT(IDNUM, id) >= 0x2;
  224. }
  225. static void macb_set_hwaddr(struct macb *bp)
  226. {
  227. u32 bottom;
  228. u16 top;
  229. bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
  230. macb_or_gem_writel(bp, SA1B, bottom);
  231. top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
  232. macb_or_gem_writel(bp, SA1T, top);
  233. /* Clear unused address register sets */
  234. macb_or_gem_writel(bp, SA2B, 0);
  235. macb_or_gem_writel(bp, SA2T, 0);
  236. macb_or_gem_writel(bp, SA3B, 0);
  237. macb_or_gem_writel(bp, SA3T, 0);
  238. macb_or_gem_writel(bp, SA4B, 0);
  239. macb_or_gem_writel(bp, SA4T, 0);
  240. }
  241. static void macb_get_hwaddr(struct macb *bp)
  242. {
  243. struct macb_platform_data *pdata;
  244. u32 bottom;
  245. u16 top;
  246. u8 addr[6];
  247. int i;
  248. pdata = dev_get_platdata(&bp->pdev->dev);
  249. /* Check all 4 address register for valid address */
  250. for (i = 0; i < 4; i++) {
  251. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  252. top = macb_or_gem_readl(bp, SA1T + i * 8);
  253. if (pdata && pdata->rev_eth_addr) {
  254. addr[5] = bottom & 0xff;
  255. addr[4] = (bottom >> 8) & 0xff;
  256. addr[3] = (bottom >> 16) & 0xff;
  257. addr[2] = (bottom >> 24) & 0xff;
  258. addr[1] = top & 0xff;
  259. addr[0] = (top & 0xff00) >> 8;
  260. } else {
  261. addr[0] = bottom & 0xff;
  262. addr[1] = (bottom >> 8) & 0xff;
  263. addr[2] = (bottom >> 16) & 0xff;
  264. addr[3] = (bottom >> 24) & 0xff;
  265. addr[4] = top & 0xff;
  266. addr[5] = (top >> 8) & 0xff;
  267. }
  268. if (is_valid_ether_addr(addr)) {
  269. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  270. return;
  271. }
  272. }
  273. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  274. eth_hw_addr_random(bp->dev);
  275. }
  276. static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  277. {
  278. struct macb *bp = bus->priv;
  279. int value;
  280. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  281. | MACB_BF(RW, MACB_MAN_READ)
  282. | MACB_BF(PHYA, mii_id)
  283. | MACB_BF(REGA, regnum)
  284. | MACB_BF(CODE, MACB_MAN_CODE)));
  285. /* wait for end of transfer */
  286. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  287. cpu_relax();
  288. value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  289. return value;
  290. }
  291. static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  292. u16 value)
  293. {
  294. struct macb *bp = bus->priv;
  295. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
  296. | MACB_BF(RW, MACB_MAN_WRITE)
  297. | MACB_BF(PHYA, mii_id)
  298. | MACB_BF(REGA, regnum)
  299. | MACB_BF(CODE, MACB_MAN_CODE)
  300. | MACB_BF(DATA, value)));
  301. /* wait for end of transfer */
  302. while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
  303. cpu_relax();
  304. return 0;
  305. }
  306. /**
  307. * macb_set_tx_clk() - Set a clock to a new frequency
  308. * @clk Pointer to the clock to change
  309. * @rate New frequency in Hz
  310. * @dev Pointer to the struct net_device
  311. */
  312. static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
  313. {
  314. long ferr, rate, rate_rounded;
  315. if (!clk)
  316. return;
  317. switch (speed) {
  318. case SPEED_10:
  319. rate = 2500000;
  320. break;
  321. case SPEED_100:
  322. rate = 25000000;
  323. break;
  324. case SPEED_1000:
  325. rate = 125000000;
  326. break;
  327. default:
  328. return;
  329. }
  330. rate_rounded = clk_round_rate(clk, rate);
  331. if (rate_rounded < 0)
  332. return;
  333. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  334. * is not satisfied.
  335. */
  336. ferr = abs(rate_rounded - rate);
  337. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  338. if (ferr > 5)
  339. netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
  340. rate);
  341. if (clk_set_rate(clk, rate_rounded))
  342. netdev_err(dev, "adjusting tx_clk failed.\n");
  343. }
  344. static void macb_handle_link_change(struct net_device *dev)
  345. {
  346. struct macb *bp = netdev_priv(dev);
  347. struct phy_device *phydev = dev->phydev;
  348. unsigned long flags;
  349. int status_change = 0;
  350. spin_lock_irqsave(&bp->lock, flags);
  351. if (phydev->link) {
  352. if ((bp->speed != phydev->speed) ||
  353. (bp->duplex != phydev->duplex)) {
  354. u32 reg;
  355. reg = macb_readl(bp, NCFGR);
  356. reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  357. if (macb_is_gem(bp))
  358. reg &= ~GEM_BIT(GBE);
  359. if (phydev->duplex)
  360. reg |= MACB_BIT(FD);
  361. if (phydev->speed == SPEED_100)
  362. reg |= MACB_BIT(SPD);
  363. if (phydev->speed == SPEED_1000 &&
  364. bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  365. reg |= GEM_BIT(GBE);
  366. macb_or_gem_writel(bp, NCFGR, reg);
  367. bp->speed = phydev->speed;
  368. bp->duplex = phydev->duplex;
  369. status_change = 1;
  370. }
  371. }
  372. if (phydev->link != bp->link) {
  373. if (!phydev->link) {
  374. bp->speed = 0;
  375. bp->duplex = -1;
  376. }
  377. bp->link = phydev->link;
  378. status_change = 1;
  379. }
  380. spin_unlock_irqrestore(&bp->lock, flags);
  381. if (status_change) {
  382. if (phydev->link) {
  383. /* Update the TX clock rate if and only if the link is
  384. * up and there has been a link change.
  385. */
  386. macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
  387. netif_carrier_on(dev);
  388. netdev_info(dev, "link up (%d/%s)\n",
  389. phydev->speed,
  390. phydev->duplex == DUPLEX_FULL ?
  391. "Full" : "Half");
  392. } else {
  393. netif_carrier_off(dev);
  394. netdev_info(dev, "link down\n");
  395. }
  396. }
  397. }
  398. /* based on au1000_eth. c*/
  399. static int macb_mii_probe(struct net_device *dev)
  400. {
  401. struct macb *bp = netdev_priv(dev);
  402. struct macb_platform_data *pdata;
  403. struct phy_device *phydev;
  404. struct device_node *np;
  405. int phy_irq, ret, i;
  406. pdata = dev_get_platdata(&bp->pdev->dev);
  407. np = bp->pdev->dev.of_node;
  408. ret = 0;
  409. if (np) {
  410. if (of_phy_is_fixed_link(np)) {
  411. if (of_phy_register_fixed_link(np) < 0) {
  412. dev_err(&bp->pdev->dev,
  413. "broken fixed-link specification\n");
  414. return -ENODEV;
  415. }
  416. bp->phy_node = of_node_get(np);
  417. } else {
  418. bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
  419. /* fallback to standard phy registration if no
  420. * phy-handle was found nor any phy found during
  421. * dt phy registration
  422. */
  423. if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
  424. for (i = 0; i < PHY_MAX_ADDR; i++) {
  425. struct phy_device *phydev;
  426. phydev = mdiobus_scan(bp->mii_bus, i);
  427. if (IS_ERR(phydev) &&
  428. PTR_ERR(phydev) != -ENODEV) {
  429. ret = PTR_ERR(phydev);
  430. break;
  431. }
  432. }
  433. if (ret)
  434. return -ENODEV;
  435. }
  436. }
  437. }
  438. if (bp->phy_node) {
  439. phydev = of_phy_connect(dev, bp->phy_node,
  440. &macb_handle_link_change, 0,
  441. bp->phy_interface);
  442. if (!phydev)
  443. return -ENODEV;
  444. } else {
  445. phydev = phy_find_first(bp->mii_bus);
  446. if (!phydev) {
  447. netdev_err(dev, "no PHY found\n");
  448. return -ENXIO;
  449. }
  450. if (pdata) {
  451. if (gpio_is_valid(pdata->phy_irq_pin)) {
  452. ret = devm_gpio_request(&bp->pdev->dev,
  453. pdata->phy_irq_pin, "phy int");
  454. if (!ret) {
  455. phy_irq = gpio_to_irq(pdata->phy_irq_pin);
  456. phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
  457. }
  458. } else {
  459. phydev->irq = PHY_POLL;
  460. }
  461. }
  462. /* attach the mac to the phy */
  463. ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
  464. bp->phy_interface);
  465. if (ret) {
  466. netdev_err(dev, "Could not attach to PHY\n");
  467. return ret;
  468. }
  469. }
  470. /* mask with MAC supported features */
  471. if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
  472. phydev->supported &= PHY_GBIT_FEATURES;
  473. else
  474. phydev->supported &= PHY_BASIC_FEATURES;
  475. if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
  476. phydev->supported &= ~SUPPORTED_1000baseT_Half;
  477. phydev->advertising = phydev->supported;
  478. bp->link = 0;
  479. bp->speed = 0;
  480. bp->duplex = -1;
  481. return 0;
  482. }
  483. static int macb_mii_init(struct macb *bp)
  484. {
  485. struct macb_platform_data *pdata;
  486. struct device_node *np;
  487. int err;
  488. /* Enable management port */
  489. macb_writel(bp, NCR, MACB_BIT(MPE));
  490. bp->mii_bus = mdiobus_alloc();
  491. if (!bp->mii_bus) {
  492. err = -ENOMEM;
  493. goto err_out;
  494. }
  495. bp->mii_bus->name = "MACB_mii_bus";
  496. bp->mii_bus->read = &macb_mdio_read;
  497. bp->mii_bus->write = &macb_mdio_write;
  498. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  499. bp->pdev->name, bp->pdev->id);
  500. bp->mii_bus->priv = bp;
  501. bp->mii_bus->parent = &bp->pdev->dev;
  502. pdata = dev_get_platdata(&bp->pdev->dev);
  503. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  504. np = bp->pdev->dev.of_node;
  505. if (pdata)
  506. bp->mii_bus->phy_mask = pdata->phy_mask;
  507. err = of_mdiobus_register(bp->mii_bus, np);
  508. if (err)
  509. goto err_out_free_mdiobus;
  510. err = macb_mii_probe(bp->dev);
  511. if (err)
  512. goto err_out_unregister_bus;
  513. return 0;
  514. err_out_unregister_bus:
  515. mdiobus_unregister(bp->mii_bus);
  516. if (np && of_phy_is_fixed_link(np))
  517. of_phy_deregister_fixed_link(np);
  518. err_out_free_mdiobus:
  519. of_node_put(bp->phy_node);
  520. mdiobus_free(bp->mii_bus);
  521. err_out:
  522. return err;
  523. }
  524. static void macb_update_stats(struct macb *bp)
  525. {
  526. u32 *p = &bp->hw_stats.macb.rx_pause_frames;
  527. u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  528. int offset = MACB_PFR;
  529. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  530. for (; p < end; p++, offset += 4)
  531. *p += bp->macb_reg_readl(bp, offset);
  532. }
  533. static int macb_halt_tx(struct macb *bp)
  534. {
  535. unsigned long halt_time, timeout;
  536. u32 status;
  537. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  538. timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
  539. do {
  540. halt_time = jiffies;
  541. status = macb_readl(bp, TSR);
  542. if (!(status & MACB_BIT(TGO)))
  543. return 0;
  544. usleep_range(10, 250);
  545. } while (time_before(halt_time, timeout));
  546. return -ETIMEDOUT;
  547. }
  548. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
  549. {
  550. if (tx_skb->mapping) {
  551. if (tx_skb->mapped_as_page)
  552. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  553. tx_skb->size, DMA_TO_DEVICE);
  554. else
  555. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  556. tx_skb->size, DMA_TO_DEVICE);
  557. tx_skb->mapping = 0;
  558. }
  559. if (tx_skb->skb) {
  560. dev_kfree_skb_any(tx_skb->skb);
  561. tx_skb->skb = NULL;
  562. }
  563. }
  564. static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
  565. {
  566. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  567. struct macb_dma_desc_64 *desc_64;
  568. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  569. desc_64 = macb_64b_desc(bp, desc);
  570. desc_64->addrh = upper_32_bits(addr);
  571. }
  572. #endif
  573. desc->addr = lower_32_bits(addr);
  574. }
  575. static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
  576. {
  577. dma_addr_t addr = 0;
  578. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  579. struct macb_dma_desc_64 *desc_64;
  580. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  581. desc_64 = macb_64b_desc(bp, desc);
  582. addr = ((u64)(desc_64->addrh) << 32);
  583. }
  584. #endif
  585. addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  586. return addr;
  587. }
  588. static void macb_tx_error_task(struct work_struct *work)
  589. {
  590. struct macb_queue *queue = container_of(work, struct macb_queue,
  591. tx_error_task);
  592. struct macb *bp = queue->bp;
  593. struct macb_tx_skb *tx_skb;
  594. struct macb_dma_desc *desc;
  595. struct sk_buff *skb;
  596. unsigned int tail;
  597. unsigned long flags;
  598. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  599. (unsigned int)(queue - bp->queues),
  600. queue->tx_tail, queue->tx_head);
  601. /* Prevent the queue IRQ handlers from running: each of them may call
  602. * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
  603. * As explained below, we have to halt the transmission before updating
  604. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  605. * network engine about the macb/gem being halted.
  606. */
  607. spin_lock_irqsave(&bp->lock, flags);
  608. /* Make sure nobody is trying to queue up new packets */
  609. netif_tx_stop_all_queues(bp->dev);
  610. /* Stop transmission now
  611. * (in case we have just queued new packets)
  612. * macb/gem must be halted to write TBQP register
  613. */
  614. if (macb_halt_tx(bp))
  615. /* Just complain for now, reinitializing TX path can be good */
  616. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  617. /* Treat frames in TX queue including the ones that caused the error.
  618. * Free transmit buffers in upper layer.
  619. */
  620. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  621. u32 ctrl;
  622. desc = macb_tx_desc(queue, tail);
  623. ctrl = desc->ctrl;
  624. tx_skb = macb_tx_skb(queue, tail);
  625. skb = tx_skb->skb;
  626. if (ctrl & MACB_BIT(TX_USED)) {
  627. /* skb is set for the last buffer of the frame */
  628. while (!skb) {
  629. macb_tx_unmap(bp, tx_skb);
  630. tail++;
  631. tx_skb = macb_tx_skb(queue, tail);
  632. skb = tx_skb->skb;
  633. }
  634. /* ctrl still refers to the first buffer descriptor
  635. * since it's the only one written back by the hardware
  636. */
  637. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  638. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  639. macb_tx_ring_wrap(bp, tail),
  640. skb->data);
  641. bp->dev->stats.tx_packets++;
  642. queue->stats.tx_packets++;
  643. bp->dev->stats.tx_bytes += skb->len;
  644. queue->stats.tx_bytes += skb->len;
  645. }
  646. } else {
  647. /* "Buffers exhausted mid-frame" errors may only happen
  648. * if the driver is buggy, so complain loudly about
  649. * those. Statistics are updated by hardware.
  650. */
  651. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  652. netdev_err(bp->dev,
  653. "BUG: TX buffers exhausted mid-frame\n");
  654. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  655. }
  656. macb_tx_unmap(bp, tx_skb);
  657. }
  658. /* Set end of TX queue */
  659. desc = macb_tx_desc(queue, 0);
  660. macb_set_addr(bp, desc, 0);
  661. desc->ctrl = MACB_BIT(TX_USED);
  662. /* Make descriptor updates visible to hardware */
  663. wmb();
  664. /* Reinitialize the TX desc queue */
  665. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  666. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  667. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  668. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  669. #endif
  670. /* Make TX ring reflect state of hardware */
  671. queue->tx_head = 0;
  672. queue->tx_tail = 0;
  673. /* Housework before enabling TX IRQ */
  674. macb_writel(bp, TSR, macb_readl(bp, TSR));
  675. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  676. /* Now we are ready to start transmission again */
  677. netif_tx_start_all_queues(bp->dev);
  678. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  679. spin_unlock_irqrestore(&bp->lock, flags);
  680. }
  681. static void macb_tx_interrupt(struct macb_queue *queue)
  682. {
  683. unsigned int tail;
  684. unsigned int head;
  685. u32 status;
  686. struct macb *bp = queue->bp;
  687. u16 queue_index = queue - bp->queues;
  688. status = macb_readl(bp, TSR);
  689. macb_writel(bp, TSR, status);
  690. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  691. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  692. netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
  693. (unsigned long)status);
  694. head = queue->tx_head;
  695. for (tail = queue->tx_tail; tail != head; tail++) {
  696. struct macb_tx_skb *tx_skb;
  697. struct sk_buff *skb;
  698. struct macb_dma_desc *desc;
  699. u32 ctrl;
  700. desc = macb_tx_desc(queue, tail);
  701. /* Make hw descriptor updates visible to CPU */
  702. rmb();
  703. ctrl = desc->ctrl;
  704. /* TX_USED bit is only set by hardware on the very first buffer
  705. * descriptor of the transmitted frame.
  706. */
  707. if (!(ctrl & MACB_BIT(TX_USED)))
  708. break;
  709. /* Process all buffers of the current transmitted frame */
  710. for (;; tail++) {
  711. tx_skb = macb_tx_skb(queue, tail);
  712. skb = tx_skb->skb;
  713. /* First, update TX stats if needed */
  714. if (skb) {
  715. if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
  716. /* skb now belongs to timestamp buffer
  717. * and will be removed later
  718. */
  719. tx_skb->skb = NULL;
  720. }
  721. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  722. macb_tx_ring_wrap(bp, tail),
  723. skb->data);
  724. bp->dev->stats.tx_packets++;
  725. queue->stats.tx_packets++;
  726. bp->dev->stats.tx_bytes += skb->len;
  727. queue->stats.tx_bytes += skb->len;
  728. }
  729. /* Now we can safely release resources */
  730. macb_tx_unmap(bp, tx_skb);
  731. /* skb is set only for the last buffer of the frame.
  732. * WARNING: at this point skb has been freed by
  733. * macb_tx_unmap().
  734. */
  735. if (skb)
  736. break;
  737. }
  738. }
  739. queue->tx_tail = tail;
  740. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  741. CIRC_CNT(queue->tx_head, queue->tx_tail,
  742. bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
  743. netif_wake_subqueue(bp->dev, queue_index);
  744. }
  745. static void gem_rx_refill(struct macb_queue *queue)
  746. {
  747. unsigned int entry;
  748. struct sk_buff *skb;
  749. dma_addr_t paddr;
  750. struct macb *bp = queue->bp;
  751. struct macb_dma_desc *desc;
  752. while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
  753. bp->rx_ring_size) > 0) {
  754. entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
  755. /* Make hw descriptor updates visible to CPU */
  756. rmb();
  757. queue->rx_prepared_head++;
  758. desc = macb_rx_desc(queue, entry);
  759. if (!queue->rx_skbuff[entry]) {
  760. /* allocate sk_buff for this free entry in ring */
  761. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  762. if (unlikely(!skb)) {
  763. netdev_err(bp->dev,
  764. "Unable to allocate sk_buff\n");
  765. break;
  766. }
  767. /* now fill corresponding descriptor entry */
  768. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  769. bp->rx_buffer_size,
  770. DMA_FROM_DEVICE);
  771. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  772. dev_kfree_skb(skb);
  773. break;
  774. }
  775. queue->rx_skbuff[entry] = skb;
  776. if (entry == bp->rx_ring_size - 1)
  777. paddr |= MACB_BIT(RX_WRAP);
  778. macb_set_addr(bp, desc, paddr);
  779. desc->ctrl = 0;
  780. /* properly align Ethernet header */
  781. skb_reserve(skb, NET_IP_ALIGN);
  782. } else {
  783. desc->addr &= ~MACB_BIT(RX_USED);
  784. desc->ctrl = 0;
  785. }
  786. }
  787. /* Make descriptor updates visible to hardware */
  788. wmb();
  789. netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
  790. queue, queue->rx_prepared_head, queue->rx_tail);
  791. }
  792. /* Mark DMA descriptors from begin up to and not including end as unused */
  793. static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
  794. unsigned int end)
  795. {
  796. unsigned int frag;
  797. for (frag = begin; frag != end; frag++) {
  798. struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
  799. desc->addr &= ~MACB_BIT(RX_USED);
  800. }
  801. /* Make descriptor updates visible to hardware */
  802. wmb();
  803. /* When this happens, the hardware stats registers for
  804. * whatever caused this is updated, so we don't have to record
  805. * anything.
  806. */
  807. }
  808. static int gem_rx(struct macb_queue *queue, int budget)
  809. {
  810. struct macb *bp = queue->bp;
  811. unsigned int len;
  812. unsigned int entry;
  813. struct sk_buff *skb;
  814. struct macb_dma_desc *desc;
  815. int count = 0;
  816. while (count < budget) {
  817. u32 ctrl;
  818. dma_addr_t addr;
  819. bool rxused;
  820. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  821. desc = macb_rx_desc(queue, entry);
  822. /* Make hw descriptor updates visible to CPU */
  823. rmb();
  824. rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
  825. addr = macb_get_addr(bp, desc);
  826. ctrl = desc->ctrl;
  827. if (!rxused)
  828. break;
  829. queue->rx_tail++;
  830. count++;
  831. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  832. netdev_err(bp->dev,
  833. "not whole frame pointed by descriptor\n");
  834. bp->dev->stats.rx_dropped++;
  835. queue->stats.rx_dropped++;
  836. break;
  837. }
  838. skb = queue->rx_skbuff[entry];
  839. if (unlikely(!skb)) {
  840. netdev_err(bp->dev,
  841. "inconsistent Rx descriptor chain\n");
  842. bp->dev->stats.rx_dropped++;
  843. queue->stats.rx_dropped++;
  844. break;
  845. }
  846. /* now everything is ready for receiving packet */
  847. queue->rx_skbuff[entry] = NULL;
  848. len = ctrl & bp->rx_frm_len_mask;
  849. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  850. skb_put(skb, len);
  851. dma_unmap_single(&bp->pdev->dev, addr,
  852. bp->rx_buffer_size, DMA_FROM_DEVICE);
  853. skb->protocol = eth_type_trans(skb, bp->dev);
  854. skb_checksum_none_assert(skb);
  855. if (bp->dev->features & NETIF_F_RXCSUM &&
  856. !(bp->dev->flags & IFF_PROMISC) &&
  857. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  858. skb->ip_summed = CHECKSUM_UNNECESSARY;
  859. bp->dev->stats.rx_packets++;
  860. queue->stats.rx_packets++;
  861. bp->dev->stats.rx_bytes += skb->len;
  862. queue->stats.rx_bytes += skb->len;
  863. gem_ptp_do_rxstamp(bp, skb, desc);
  864. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  865. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  866. skb->len, skb->csum);
  867. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  868. skb_mac_header(skb), 16, true);
  869. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  870. skb->data, 32, true);
  871. #endif
  872. netif_receive_skb(skb);
  873. }
  874. gem_rx_refill(queue);
  875. return count;
  876. }
  877. static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
  878. unsigned int last_frag)
  879. {
  880. unsigned int len;
  881. unsigned int frag;
  882. unsigned int offset;
  883. struct sk_buff *skb;
  884. struct macb_dma_desc *desc;
  885. struct macb *bp = queue->bp;
  886. desc = macb_rx_desc(queue, last_frag);
  887. len = desc->ctrl & bp->rx_frm_len_mask;
  888. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  889. macb_rx_ring_wrap(bp, first_frag),
  890. macb_rx_ring_wrap(bp, last_frag), len);
  891. /* The ethernet header starts NET_IP_ALIGN bytes into the
  892. * first buffer. Since the header is 14 bytes, this makes the
  893. * payload word-aligned.
  894. *
  895. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  896. * the two padding bytes into the skb so that we avoid hitting
  897. * the slowpath in memcpy(), and pull them off afterwards.
  898. */
  899. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  900. if (!skb) {
  901. bp->dev->stats.rx_dropped++;
  902. for (frag = first_frag; ; frag++) {
  903. desc = macb_rx_desc(queue, frag);
  904. desc->addr &= ~MACB_BIT(RX_USED);
  905. if (frag == last_frag)
  906. break;
  907. }
  908. /* Make descriptor updates visible to hardware */
  909. wmb();
  910. return 1;
  911. }
  912. offset = 0;
  913. len += NET_IP_ALIGN;
  914. skb_checksum_none_assert(skb);
  915. skb_put(skb, len);
  916. for (frag = first_frag; ; frag++) {
  917. unsigned int frag_len = bp->rx_buffer_size;
  918. if (offset + frag_len > len) {
  919. if (unlikely(frag != last_frag)) {
  920. dev_kfree_skb_any(skb);
  921. return -1;
  922. }
  923. frag_len = len - offset;
  924. }
  925. skb_copy_to_linear_data_offset(skb, offset,
  926. macb_rx_buffer(queue, frag),
  927. frag_len);
  928. offset += bp->rx_buffer_size;
  929. desc = macb_rx_desc(queue, frag);
  930. desc->addr &= ~MACB_BIT(RX_USED);
  931. if (frag == last_frag)
  932. break;
  933. }
  934. /* Make descriptor updates visible to hardware */
  935. wmb();
  936. __skb_pull(skb, NET_IP_ALIGN);
  937. skb->protocol = eth_type_trans(skb, bp->dev);
  938. bp->dev->stats.rx_packets++;
  939. bp->dev->stats.rx_bytes += skb->len;
  940. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  941. skb->len, skb->csum);
  942. netif_receive_skb(skb);
  943. return 0;
  944. }
  945. static inline void macb_init_rx_ring(struct macb_queue *queue)
  946. {
  947. struct macb *bp = queue->bp;
  948. dma_addr_t addr;
  949. struct macb_dma_desc *desc = NULL;
  950. int i;
  951. addr = queue->rx_buffers_dma;
  952. for (i = 0; i < bp->rx_ring_size; i++) {
  953. desc = macb_rx_desc(queue, i);
  954. macb_set_addr(bp, desc, addr);
  955. desc->ctrl = 0;
  956. addr += bp->rx_buffer_size;
  957. }
  958. desc->addr |= MACB_BIT(RX_WRAP);
  959. queue->rx_tail = 0;
  960. }
  961. static int macb_rx(struct macb_queue *queue, int budget)
  962. {
  963. struct macb *bp = queue->bp;
  964. bool reset_rx_queue = false;
  965. int received = 0;
  966. unsigned int tail;
  967. int first_frag = -1;
  968. for (tail = queue->rx_tail; budget > 0; tail++) {
  969. struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
  970. u32 ctrl;
  971. /* Make hw descriptor updates visible to CPU */
  972. rmb();
  973. ctrl = desc->ctrl;
  974. if (!(desc->addr & MACB_BIT(RX_USED)))
  975. break;
  976. if (ctrl & MACB_BIT(RX_SOF)) {
  977. if (first_frag != -1)
  978. discard_partial_frame(queue, first_frag, tail);
  979. first_frag = tail;
  980. }
  981. if (ctrl & MACB_BIT(RX_EOF)) {
  982. int dropped;
  983. if (unlikely(first_frag == -1)) {
  984. reset_rx_queue = true;
  985. continue;
  986. }
  987. dropped = macb_rx_frame(queue, first_frag, tail);
  988. first_frag = -1;
  989. if (unlikely(dropped < 0)) {
  990. reset_rx_queue = true;
  991. continue;
  992. }
  993. if (!dropped) {
  994. received++;
  995. budget--;
  996. }
  997. }
  998. }
  999. if (unlikely(reset_rx_queue)) {
  1000. unsigned long flags;
  1001. u32 ctrl;
  1002. netdev_err(bp->dev, "RX queue corruption: reset it\n");
  1003. spin_lock_irqsave(&bp->lock, flags);
  1004. ctrl = macb_readl(bp, NCR);
  1005. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1006. macb_init_rx_ring(queue);
  1007. queue_writel(queue, RBQP, queue->rx_ring_dma);
  1008. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1009. spin_unlock_irqrestore(&bp->lock, flags);
  1010. return received;
  1011. }
  1012. if (first_frag != -1)
  1013. queue->rx_tail = first_frag;
  1014. else
  1015. queue->rx_tail = tail;
  1016. return received;
  1017. }
  1018. static int macb_poll(struct napi_struct *napi, int budget)
  1019. {
  1020. struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
  1021. struct macb *bp = queue->bp;
  1022. int work_done;
  1023. u32 status;
  1024. status = macb_readl(bp, RSR);
  1025. macb_writel(bp, RSR, status);
  1026. netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
  1027. (unsigned long)status, budget);
  1028. work_done = bp->macbgem_ops.mog_rx(queue, budget);
  1029. if (work_done < budget) {
  1030. napi_complete_done(napi, work_done);
  1031. /* Packets received while interrupts were disabled */
  1032. status = macb_readl(bp, RSR);
  1033. if (status) {
  1034. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1035. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1036. napi_reschedule(napi);
  1037. } else {
  1038. queue_writel(queue, IER, MACB_RX_INT_FLAGS);
  1039. }
  1040. }
  1041. /* TODO: Handle errors */
  1042. return work_done;
  1043. }
  1044. static void macb_hresp_error_task(unsigned long data)
  1045. {
  1046. struct macb *bp = (struct macb *)data;
  1047. struct net_device *dev = bp->dev;
  1048. struct macb_queue *queue = bp->queues;
  1049. unsigned int q;
  1050. u32 ctrl;
  1051. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1052. queue_writel(queue, IDR, MACB_RX_INT_FLAGS |
  1053. MACB_TX_INT_FLAGS |
  1054. MACB_BIT(HRESP));
  1055. }
  1056. ctrl = macb_readl(bp, NCR);
  1057. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  1058. macb_writel(bp, NCR, ctrl);
  1059. netif_tx_stop_all_queues(dev);
  1060. netif_carrier_off(dev);
  1061. bp->macbgem_ops.mog_init_rings(bp);
  1062. /* Initialize TX and RX buffers */
  1063. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1064. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  1065. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1066. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1067. queue_writel(queue, RBQPH,
  1068. upper_32_bits(queue->rx_ring_dma));
  1069. #endif
  1070. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1071. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1072. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1073. queue_writel(queue, TBQPH,
  1074. upper_32_bits(queue->tx_ring_dma));
  1075. #endif
  1076. /* Enable interrupts */
  1077. queue_writel(queue, IER,
  1078. MACB_RX_INT_FLAGS |
  1079. MACB_TX_INT_FLAGS |
  1080. MACB_BIT(HRESP));
  1081. }
  1082. ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
  1083. macb_writel(bp, NCR, ctrl);
  1084. netif_carrier_on(dev);
  1085. netif_tx_start_all_queues(dev);
  1086. }
  1087. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  1088. {
  1089. struct macb_queue *queue = dev_id;
  1090. struct macb *bp = queue->bp;
  1091. struct net_device *dev = bp->dev;
  1092. u32 status, ctrl;
  1093. status = queue_readl(queue, ISR);
  1094. if (unlikely(!status))
  1095. return IRQ_NONE;
  1096. spin_lock(&bp->lock);
  1097. while (status) {
  1098. /* close possible race with dev_close */
  1099. if (unlikely(!netif_running(dev))) {
  1100. queue_writel(queue, IDR, -1);
  1101. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1102. queue_writel(queue, ISR, -1);
  1103. break;
  1104. }
  1105. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  1106. (unsigned int)(queue - bp->queues),
  1107. (unsigned long)status);
  1108. if (status & MACB_RX_INT_FLAGS) {
  1109. /* There's no point taking any more interrupts
  1110. * until we have processed the buffers. The
  1111. * scheduling call may fail if the poll routine
  1112. * is already scheduled, so disable interrupts
  1113. * now.
  1114. */
  1115. queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
  1116. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1117. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1118. if (napi_schedule_prep(&queue->napi)) {
  1119. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  1120. __napi_schedule(&queue->napi);
  1121. }
  1122. }
  1123. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  1124. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  1125. schedule_work(&queue->tx_error_task);
  1126. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1127. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  1128. break;
  1129. }
  1130. if (status & MACB_BIT(TCOMP))
  1131. macb_tx_interrupt(queue);
  1132. /* Link change detection isn't possible with RMII, so we'll
  1133. * add that if/when we get our hands on a full-blown MII PHY.
  1134. */
  1135. /* There is a hardware issue under heavy load where DMA can
  1136. * stop, this causes endless "used buffer descriptor read"
  1137. * interrupts but it can be cleared by re-enabling RX. See
  1138. * the at91 manual, section 41.3.1 or the Zynq manual
  1139. * section 16.7.4 for details.
  1140. */
  1141. if (status & MACB_BIT(RXUBR)) {
  1142. ctrl = macb_readl(bp, NCR);
  1143. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1144. wmb();
  1145. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1146. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1147. queue_writel(queue, ISR, MACB_BIT(RXUBR));
  1148. }
  1149. if (status & MACB_BIT(ISR_ROVR)) {
  1150. /* We missed at least one packet */
  1151. if (macb_is_gem(bp))
  1152. bp->hw_stats.gem.rx_overruns++;
  1153. else
  1154. bp->hw_stats.macb.rx_overruns++;
  1155. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1156. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  1157. }
  1158. if (status & MACB_BIT(HRESP)) {
  1159. tasklet_schedule(&bp->hresp_err_tasklet);
  1160. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  1161. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1162. queue_writel(queue, ISR, MACB_BIT(HRESP));
  1163. }
  1164. status = queue_readl(queue, ISR);
  1165. }
  1166. spin_unlock(&bp->lock);
  1167. return IRQ_HANDLED;
  1168. }
  1169. #ifdef CONFIG_NET_POLL_CONTROLLER
  1170. /* Polling receive - used by netconsole and other diagnostic tools
  1171. * to allow network i/o with interrupts disabled.
  1172. */
  1173. static void macb_poll_controller(struct net_device *dev)
  1174. {
  1175. struct macb *bp = netdev_priv(dev);
  1176. struct macb_queue *queue;
  1177. unsigned long flags;
  1178. unsigned int q;
  1179. local_irq_save(flags);
  1180. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1181. macb_interrupt(dev->irq, queue);
  1182. local_irq_restore(flags);
  1183. }
  1184. #endif
  1185. static unsigned int macb_tx_map(struct macb *bp,
  1186. struct macb_queue *queue,
  1187. struct sk_buff *skb,
  1188. unsigned int hdrlen)
  1189. {
  1190. dma_addr_t mapping;
  1191. unsigned int len, entry, i, tx_head = queue->tx_head;
  1192. struct macb_tx_skb *tx_skb = NULL;
  1193. struct macb_dma_desc *desc;
  1194. unsigned int offset, size, count = 0;
  1195. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  1196. unsigned int eof = 1, mss_mfs = 0;
  1197. u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
  1198. /* LSO */
  1199. if (skb_shinfo(skb)->gso_size != 0) {
  1200. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1201. /* UDP - UFO */
  1202. lso_ctrl = MACB_LSO_UFO_ENABLE;
  1203. else
  1204. /* TCP - TSO */
  1205. lso_ctrl = MACB_LSO_TSO_ENABLE;
  1206. }
  1207. /* First, map non-paged data */
  1208. len = skb_headlen(skb);
  1209. /* first buffer length */
  1210. size = hdrlen;
  1211. offset = 0;
  1212. while (len) {
  1213. entry = macb_tx_ring_wrap(bp, tx_head);
  1214. tx_skb = &queue->tx_skb[entry];
  1215. mapping = dma_map_single(&bp->pdev->dev,
  1216. skb->data + offset,
  1217. size, DMA_TO_DEVICE);
  1218. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1219. goto dma_error;
  1220. /* Save info to properly release resources */
  1221. tx_skb->skb = NULL;
  1222. tx_skb->mapping = mapping;
  1223. tx_skb->size = size;
  1224. tx_skb->mapped_as_page = false;
  1225. len -= size;
  1226. offset += size;
  1227. count++;
  1228. tx_head++;
  1229. size = min(len, bp->max_tx_length);
  1230. }
  1231. /* Then, map paged data from fragments */
  1232. for (f = 0; f < nr_frags; f++) {
  1233. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1234. len = skb_frag_size(frag);
  1235. offset = 0;
  1236. while (len) {
  1237. size = min(len, bp->max_tx_length);
  1238. entry = macb_tx_ring_wrap(bp, tx_head);
  1239. tx_skb = &queue->tx_skb[entry];
  1240. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  1241. offset, size, DMA_TO_DEVICE);
  1242. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1243. goto dma_error;
  1244. /* Save info to properly release resources */
  1245. tx_skb->skb = NULL;
  1246. tx_skb->mapping = mapping;
  1247. tx_skb->size = size;
  1248. tx_skb->mapped_as_page = true;
  1249. len -= size;
  1250. offset += size;
  1251. count++;
  1252. tx_head++;
  1253. }
  1254. }
  1255. /* Should never happen */
  1256. if (unlikely(!tx_skb)) {
  1257. netdev_err(bp->dev, "BUG! empty skb!\n");
  1258. return 0;
  1259. }
  1260. /* This is the last buffer of the frame: save socket buffer */
  1261. tx_skb->skb = skb;
  1262. /* Update TX ring: update buffer descriptors in reverse order
  1263. * to avoid race condition
  1264. */
  1265. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1266. * to set the end of TX queue
  1267. */
  1268. i = tx_head;
  1269. entry = macb_tx_ring_wrap(bp, i);
  1270. ctrl = MACB_BIT(TX_USED);
  1271. desc = macb_tx_desc(queue, entry);
  1272. desc->ctrl = ctrl;
  1273. if (lso_ctrl) {
  1274. if (lso_ctrl == MACB_LSO_UFO_ENABLE)
  1275. /* include header and FCS in value given to h/w */
  1276. mss_mfs = skb_shinfo(skb)->gso_size +
  1277. skb_transport_offset(skb) +
  1278. ETH_FCS_LEN;
  1279. else /* TSO */ {
  1280. mss_mfs = skb_shinfo(skb)->gso_size;
  1281. /* TCP Sequence Number Source Select
  1282. * can be set only for TSO
  1283. */
  1284. seq_ctrl = 0;
  1285. }
  1286. }
  1287. do {
  1288. i--;
  1289. entry = macb_tx_ring_wrap(bp, i);
  1290. tx_skb = &queue->tx_skb[entry];
  1291. desc = macb_tx_desc(queue, entry);
  1292. ctrl = (u32)tx_skb->size;
  1293. if (eof) {
  1294. ctrl |= MACB_BIT(TX_LAST);
  1295. eof = 0;
  1296. }
  1297. if (unlikely(entry == (bp->tx_ring_size - 1)))
  1298. ctrl |= MACB_BIT(TX_WRAP);
  1299. /* First descriptor is header descriptor */
  1300. if (i == queue->tx_head) {
  1301. ctrl |= MACB_BF(TX_LSO, lso_ctrl);
  1302. ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
  1303. } else
  1304. /* Only set MSS/MFS on payload descriptors
  1305. * (second or later descriptor)
  1306. */
  1307. ctrl |= MACB_BF(MSS_MFS, mss_mfs);
  1308. /* Set TX buffer descriptor */
  1309. macb_set_addr(bp, desc, tx_skb->mapping);
  1310. /* desc->addr must be visible to hardware before clearing
  1311. * 'TX_USED' bit in desc->ctrl.
  1312. */
  1313. wmb();
  1314. desc->ctrl = ctrl;
  1315. } while (i != queue->tx_head);
  1316. queue->tx_head = tx_head;
  1317. return count;
  1318. dma_error:
  1319. netdev_err(bp->dev, "TX DMA map failed\n");
  1320. for (i = queue->tx_head; i != tx_head; i++) {
  1321. tx_skb = macb_tx_skb(queue, i);
  1322. macb_tx_unmap(bp, tx_skb);
  1323. }
  1324. return 0;
  1325. }
  1326. static netdev_features_t macb_features_check(struct sk_buff *skb,
  1327. struct net_device *dev,
  1328. netdev_features_t features)
  1329. {
  1330. unsigned int nr_frags, f;
  1331. unsigned int hdrlen;
  1332. /* Validate LSO compatibility */
  1333. /* there is only one buffer */
  1334. if (!skb_is_nonlinear(skb))
  1335. return features;
  1336. /* length of header */
  1337. hdrlen = skb_transport_offset(skb);
  1338. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  1339. hdrlen += tcp_hdrlen(skb);
  1340. /* For LSO:
  1341. * When software supplies two or more payload buffers all payload buffers
  1342. * apart from the last must be a multiple of 8 bytes in size.
  1343. */
  1344. if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
  1345. return features & ~MACB_NETIF_LSO;
  1346. nr_frags = skb_shinfo(skb)->nr_frags;
  1347. /* No need to check last fragment */
  1348. nr_frags--;
  1349. for (f = 0; f < nr_frags; f++) {
  1350. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1351. if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
  1352. return features & ~MACB_NETIF_LSO;
  1353. }
  1354. return features;
  1355. }
  1356. static inline int macb_clear_csum(struct sk_buff *skb)
  1357. {
  1358. /* no change for packets without checksum offloading */
  1359. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1360. return 0;
  1361. /* make sure we can modify the header */
  1362. if (unlikely(skb_cow_head(skb, 0)))
  1363. return -1;
  1364. /* initialize checksum field
  1365. * This is required - at least for Zynq, which otherwise calculates
  1366. * wrong UDP header checksums for UDP packets with UDP data len <=2
  1367. */
  1368. *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
  1369. return 0;
  1370. }
  1371. static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1372. {
  1373. u16 queue_index = skb_get_queue_mapping(skb);
  1374. struct macb *bp = netdev_priv(dev);
  1375. struct macb_queue *queue = &bp->queues[queue_index];
  1376. unsigned long flags;
  1377. unsigned int desc_cnt, nr_frags, frag_size, f;
  1378. unsigned int hdrlen;
  1379. bool is_lso, is_udp = 0;
  1380. is_lso = (skb_shinfo(skb)->gso_size != 0);
  1381. if (is_lso) {
  1382. is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
  1383. /* length of headers */
  1384. if (is_udp)
  1385. /* only queue eth + ip headers separately for UDP */
  1386. hdrlen = skb_transport_offset(skb);
  1387. else
  1388. hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1389. if (skb_headlen(skb) < hdrlen) {
  1390. netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
  1391. /* if this is required, would need to copy to single buffer */
  1392. return NETDEV_TX_BUSY;
  1393. }
  1394. } else
  1395. hdrlen = min(skb_headlen(skb), bp->max_tx_length);
  1396. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1397. netdev_vdbg(bp->dev,
  1398. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1399. queue_index, skb->len, skb->head, skb->data,
  1400. skb_tail_pointer(skb), skb_end_pointer(skb));
  1401. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1402. skb->data, 16, true);
  1403. #endif
  1404. /* Count how many TX buffer descriptors are needed to send this
  1405. * socket buffer: skb fragments of jumbo frames may need to be
  1406. * split into many buffer descriptors.
  1407. */
  1408. if (is_lso && (skb_headlen(skb) > hdrlen))
  1409. /* extra header descriptor if also payload in first buffer */
  1410. desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
  1411. else
  1412. desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1413. nr_frags = skb_shinfo(skb)->nr_frags;
  1414. for (f = 0; f < nr_frags; f++) {
  1415. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1416. desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1417. }
  1418. spin_lock_irqsave(&bp->lock, flags);
  1419. /* This is a hard error, log it. */
  1420. if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
  1421. bp->tx_ring_size) < desc_cnt) {
  1422. netif_stop_subqueue(dev, queue_index);
  1423. spin_unlock_irqrestore(&bp->lock, flags);
  1424. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1425. queue->tx_head, queue->tx_tail);
  1426. return NETDEV_TX_BUSY;
  1427. }
  1428. if (macb_clear_csum(skb)) {
  1429. dev_kfree_skb_any(skb);
  1430. goto unlock;
  1431. }
  1432. /* Map socket buffer for DMA transfer */
  1433. if (!macb_tx_map(bp, queue, skb, hdrlen)) {
  1434. dev_kfree_skb_any(skb);
  1435. goto unlock;
  1436. }
  1437. /* Make newly initialized descriptor visible to hardware */
  1438. wmb();
  1439. skb_tx_timestamp(skb);
  1440. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1441. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
  1442. netif_stop_subqueue(dev, queue_index);
  1443. unlock:
  1444. spin_unlock_irqrestore(&bp->lock, flags);
  1445. return NETDEV_TX_OK;
  1446. }
  1447. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  1448. {
  1449. if (!macb_is_gem(bp)) {
  1450. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  1451. } else {
  1452. bp->rx_buffer_size = size;
  1453. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  1454. netdev_dbg(bp->dev,
  1455. "RX buffer must be multiple of %d bytes, expanding\n",
  1456. RX_BUFFER_MULTIPLE);
  1457. bp->rx_buffer_size =
  1458. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  1459. }
  1460. }
  1461. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
  1462. bp->dev->mtu, bp->rx_buffer_size);
  1463. }
  1464. static void gem_free_rx_buffers(struct macb *bp)
  1465. {
  1466. struct sk_buff *skb;
  1467. struct macb_dma_desc *desc;
  1468. struct macb_queue *queue;
  1469. dma_addr_t addr;
  1470. unsigned int q;
  1471. int i;
  1472. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1473. if (!queue->rx_skbuff)
  1474. continue;
  1475. for (i = 0; i < bp->rx_ring_size; i++) {
  1476. skb = queue->rx_skbuff[i];
  1477. if (!skb)
  1478. continue;
  1479. desc = macb_rx_desc(queue, i);
  1480. addr = macb_get_addr(bp, desc);
  1481. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  1482. DMA_FROM_DEVICE);
  1483. dev_kfree_skb_any(skb);
  1484. skb = NULL;
  1485. }
  1486. kfree(queue->rx_skbuff);
  1487. queue->rx_skbuff = NULL;
  1488. }
  1489. }
  1490. static void macb_free_rx_buffers(struct macb *bp)
  1491. {
  1492. struct macb_queue *queue = &bp->queues[0];
  1493. if (queue->rx_buffers) {
  1494. dma_free_coherent(&bp->pdev->dev,
  1495. bp->rx_ring_size * bp->rx_buffer_size,
  1496. queue->rx_buffers, queue->rx_buffers_dma);
  1497. queue->rx_buffers = NULL;
  1498. }
  1499. }
  1500. static void macb_free_consistent(struct macb *bp)
  1501. {
  1502. struct macb_queue *queue;
  1503. unsigned int q;
  1504. queue = &bp->queues[0];
  1505. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1506. if (queue->rx_ring) {
  1507. dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
  1508. queue->rx_ring, queue->rx_ring_dma);
  1509. queue->rx_ring = NULL;
  1510. }
  1511. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1512. kfree(queue->tx_skb);
  1513. queue->tx_skb = NULL;
  1514. if (queue->tx_ring) {
  1515. dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
  1516. queue->tx_ring, queue->tx_ring_dma);
  1517. queue->tx_ring = NULL;
  1518. }
  1519. }
  1520. }
  1521. static int gem_alloc_rx_buffers(struct macb *bp)
  1522. {
  1523. struct macb_queue *queue;
  1524. unsigned int q;
  1525. int size;
  1526. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1527. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  1528. queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1529. if (!queue->rx_skbuff)
  1530. return -ENOMEM;
  1531. else
  1532. netdev_dbg(bp->dev,
  1533. "Allocated %d RX struct sk_buff entries at %p\n",
  1534. bp->rx_ring_size, queue->rx_skbuff);
  1535. }
  1536. return 0;
  1537. }
  1538. static int macb_alloc_rx_buffers(struct macb *bp)
  1539. {
  1540. struct macb_queue *queue = &bp->queues[0];
  1541. int size;
  1542. size = bp->rx_ring_size * bp->rx_buffer_size;
  1543. queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1544. &queue->rx_buffers_dma, GFP_KERNEL);
  1545. if (!queue->rx_buffers)
  1546. return -ENOMEM;
  1547. netdev_dbg(bp->dev,
  1548. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1549. size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
  1550. return 0;
  1551. }
  1552. static int macb_alloc_consistent(struct macb *bp)
  1553. {
  1554. struct macb_queue *queue;
  1555. unsigned int q;
  1556. int size;
  1557. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1558. size = TX_RING_BYTES(bp);
  1559. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1560. &queue->tx_ring_dma,
  1561. GFP_KERNEL);
  1562. if (!queue->tx_ring)
  1563. goto out_err;
  1564. netdev_dbg(bp->dev,
  1565. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1566. q, size, (unsigned long)queue->tx_ring_dma,
  1567. queue->tx_ring);
  1568. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  1569. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1570. if (!queue->tx_skb)
  1571. goto out_err;
  1572. size = RX_RING_BYTES(bp);
  1573. queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1574. &queue->rx_ring_dma, GFP_KERNEL);
  1575. if (!queue->rx_ring)
  1576. goto out_err;
  1577. netdev_dbg(bp->dev,
  1578. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1579. size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
  1580. }
  1581. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1582. goto out_err;
  1583. return 0;
  1584. out_err:
  1585. macb_free_consistent(bp);
  1586. return -ENOMEM;
  1587. }
  1588. static void gem_init_rings(struct macb *bp)
  1589. {
  1590. struct macb_queue *queue;
  1591. struct macb_dma_desc *desc = NULL;
  1592. unsigned int q;
  1593. int i;
  1594. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1595. for (i = 0; i < bp->tx_ring_size; i++) {
  1596. desc = macb_tx_desc(queue, i);
  1597. macb_set_addr(bp, desc, 0);
  1598. desc->ctrl = MACB_BIT(TX_USED);
  1599. }
  1600. desc->ctrl |= MACB_BIT(TX_WRAP);
  1601. queue->tx_head = 0;
  1602. queue->tx_tail = 0;
  1603. queue->rx_tail = 0;
  1604. queue->rx_prepared_head = 0;
  1605. gem_rx_refill(queue);
  1606. }
  1607. }
  1608. static void macb_init_rings(struct macb *bp)
  1609. {
  1610. int i;
  1611. struct macb_dma_desc *desc = NULL;
  1612. macb_init_rx_ring(&bp->queues[0]);
  1613. for (i = 0; i < bp->tx_ring_size; i++) {
  1614. desc = macb_tx_desc(&bp->queues[0], i);
  1615. macb_set_addr(bp, desc, 0);
  1616. desc->ctrl = MACB_BIT(TX_USED);
  1617. }
  1618. bp->queues[0].tx_head = 0;
  1619. bp->queues[0].tx_tail = 0;
  1620. desc->ctrl |= MACB_BIT(TX_WRAP);
  1621. }
  1622. static void macb_reset_hw(struct macb *bp)
  1623. {
  1624. struct macb_queue *queue;
  1625. unsigned int q;
  1626. /* Disable RX and TX (XXX: Should we halt the transmission
  1627. * more gracefully?)
  1628. */
  1629. macb_writel(bp, NCR, 0);
  1630. /* Clear the stats registers (XXX: Update stats first?) */
  1631. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1632. /* Clear all status flags */
  1633. macb_writel(bp, TSR, -1);
  1634. macb_writel(bp, RSR, -1);
  1635. /* Disable all interrupts */
  1636. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1637. queue_writel(queue, IDR, -1);
  1638. queue_readl(queue, ISR);
  1639. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1640. queue_writel(queue, ISR, -1);
  1641. }
  1642. }
  1643. static u32 gem_mdc_clk_div(struct macb *bp)
  1644. {
  1645. u32 config;
  1646. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1647. if (pclk_hz <= 20000000)
  1648. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1649. else if (pclk_hz <= 40000000)
  1650. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1651. else if (pclk_hz <= 80000000)
  1652. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1653. else if (pclk_hz <= 120000000)
  1654. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1655. else if (pclk_hz <= 160000000)
  1656. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1657. else
  1658. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1659. return config;
  1660. }
  1661. static u32 macb_mdc_clk_div(struct macb *bp)
  1662. {
  1663. u32 config;
  1664. unsigned long pclk_hz;
  1665. if (macb_is_gem(bp))
  1666. return gem_mdc_clk_div(bp);
  1667. pclk_hz = clk_get_rate(bp->pclk);
  1668. if (pclk_hz <= 20000000)
  1669. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1670. else if (pclk_hz <= 40000000)
  1671. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1672. else if (pclk_hz <= 80000000)
  1673. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1674. else
  1675. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1676. return config;
  1677. }
  1678. /* Get the DMA bus width field of the network configuration register that we
  1679. * should program. We find the width from decoding the design configuration
  1680. * register to find the maximum supported data bus width.
  1681. */
  1682. static u32 macb_dbw(struct macb *bp)
  1683. {
  1684. if (!macb_is_gem(bp))
  1685. return 0;
  1686. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1687. case 4:
  1688. return GEM_BF(DBW, GEM_DBW128);
  1689. case 2:
  1690. return GEM_BF(DBW, GEM_DBW64);
  1691. case 1:
  1692. default:
  1693. return GEM_BF(DBW, GEM_DBW32);
  1694. }
  1695. }
  1696. /* Configure the receive DMA engine
  1697. * - use the correct receive buffer size
  1698. * - set best burst length for DMA operations
  1699. * (if not supported by FIFO, it will fallback to default)
  1700. * - set both rx/tx packet buffers to full memory size
  1701. * These are configurable parameters for GEM.
  1702. */
  1703. static void macb_configure_dma(struct macb *bp)
  1704. {
  1705. struct macb_queue *queue;
  1706. u32 buffer_size;
  1707. unsigned int q;
  1708. u32 dmacfg;
  1709. buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
  1710. if (macb_is_gem(bp)) {
  1711. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1712. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1713. if (q)
  1714. queue_writel(queue, RBQS, buffer_size);
  1715. else
  1716. dmacfg |= GEM_BF(RXBS, buffer_size);
  1717. }
  1718. if (bp->dma_burst_length)
  1719. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1720. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1721. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1722. if (bp->native_io)
  1723. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1724. else
  1725. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1726. if (bp->dev->features & NETIF_F_HW_CSUM)
  1727. dmacfg |= GEM_BIT(TXCOEN);
  1728. else
  1729. dmacfg &= ~GEM_BIT(TXCOEN);
  1730. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1731. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1732. dmacfg |= GEM_BIT(ADDR64);
  1733. #endif
  1734. #ifdef CONFIG_MACB_USE_HWSTAMP
  1735. if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
  1736. dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
  1737. #endif
  1738. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1739. dmacfg);
  1740. gem_writel(bp, DMACFG, dmacfg);
  1741. }
  1742. }
  1743. static void macb_init_hw(struct macb *bp)
  1744. {
  1745. struct macb_queue *queue;
  1746. unsigned int q;
  1747. u32 config;
  1748. macb_reset_hw(bp);
  1749. macb_set_hwaddr(bp);
  1750. config = macb_mdc_clk_div(bp);
  1751. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  1752. config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  1753. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1754. config |= MACB_BIT(PAE); /* PAuse Enable */
  1755. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1756. if (bp->caps & MACB_CAPS_JUMBO)
  1757. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  1758. else
  1759. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1760. if (bp->dev->flags & IFF_PROMISC)
  1761. config |= MACB_BIT(CAF); /* Copy All Frames */
  1762. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1763. config |= GEM_BIT(RXCOEN);
  1764. if (!(bp->dev->flags & IFF_BROADCAST))
  1765. config |= MACB_BIT(NBC); /* No BroadCast */
  1766. config |= macb_dbw(bp);
  1767. macb_writel(bp, NCFGR, config);
  1768. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  1769. gem_writel(bp, JML, bp->jumbo_max_len);
  1770. bp->speed = SPEED_10;
  1771. bp->duplex = DUPLEX_HALF;
  1772. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  1773. if (bp->caps & MACB_CAPS_JUMBO)
  1774. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  1775. macb_configure_dma(bp);
  1776. /* Initialize TX and RX buffers */
  1777. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1778. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  1779. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1780. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1781. queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
  1782. #endif
  1783. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1784. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1785. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1786. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  1787. #endif
  1788. /* Enable interrupts */
  1789. queue_writel(queue, IER,
  1790. MACB_RX_INT_FLAGS |
  1791. MACB_TX_INT_FLAGS |
  1792. MACB_BIT(HRESP));
  1793. }
  1794. /* Enable TX and RX */
  1795. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1796. }
  1797. /* The hash address register is 64 bits long and takes up two
  1798. * locations in the memory map. The least significant bits are stored
  1799. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1800. *
  1801. * The unicast hash enable and the multicast hash enable bits in the
  1802. * network configuration register enable the reception of hash matched
  1803. * frames. The destination address is reduced to a 6 bit index into
  1804. * the 64 bit hash register using the following hash function. The
  1805. * hash function is an exclusive or of every sixth bit of the
  1806. * destination address.
  1807. *
  1808. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1809. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1810. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1811. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1812. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1813. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1814. *
  1815. * da[0] represents the least significant bit of the first byte
  1816. * received, that is, the multicast/unicast indicator, and da[47]
  1817. * represents the most significant bit of the last byte received. If
  1818. * the hash index, hi[n], points to a bit that is set in the hash
  1819. * register then the frame will be matched according to whether the
  1820. * frame is multicast or unicast. A multicast match will be signalled
  1821. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1822. * index points to a bit set in the hash register. A unicast match
  1823. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1824. * and the hash index points to a bit set in the hash register. To
  1825. * receive all multicast frames, the hash register should be set with
  1826. * all ones and the multicast hash enable bit should be set in the
  1827. * network configuration register.
  1828. */
  1829. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1830. {
  1831. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1832. return 1;
  1833. return 0;
  1834. }
  1835. /* Return the hash index value for the specified address. */
  1836. static int hash_get_index(__u8 *addr)
  1837. {
  1838. int i, j, bitval;
  1839. int hash_index = 0;
  1840. for (j = 0; j < 6; j++) {
  1841. for (i = 0, bitval = 0; i < 8; i++)
  1842. bitval ^= hash_bit_value(i * 6 + j, addr);
  1843. hash_index |= (bitval << j);
  1844. }
  1845. return hash_index;
  1846. }
  1847. /* Add multicast addresses to the internal multicast-hash table. */
  1848. static void macb_sethashtable(struct net_device *dev)
  1849. {
  1850. struct netdev_hw_addr *ha;
  1851. unsigned long mc_filter[2];
  1852. unsigned int bitnr;
  1853. struct macb *bp = netdev_priv(dev);
  1854. mc_filter[0] = 0;
  1855. mc_filter[1] = 0;
  1856. netdev_for_each_mc_addr(ha, dev) {
  1857. bitnr = hash_get_index(ha->addr);
  1858. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1859. }
  1860. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1861. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1862. }
  1863. /* Enable/Disable promiscuous and multicast modes. */
  1864. static void macb_set_rx_mode(struct net_device *dev)
  1865. {
  1866. unsigned long cfg;
  1867. struct macb *bp = netdev_priv(dev);
  1868. cfg = macb_readl(bp, NCFGR);
  1869. if (dev->flags & IFF_PROMISC) {
  1870. /* Enable promiscuous mode */
  1871. cfg |= MACB_BIT(CAF);
  1872. /* Disable RX checksum offload */
  1873. if (macb_is_gem(bp))
  1874. cfg &= ~GEM_BIT(RXCOEN);
  1875. } else {
  1876. /* Disable promiscuous mode */
  1877. cfg &= ~MACB_BIT(CAF);
  1878. /* Enable RX checksum offload only if requested */
  1879. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1880. cfg |= GEM_BIT(RXCOEN);
  1881. }
  1882. if (dev->flags & IFF_ALLMULTI) {
  1883. /* Enable all multicast mode */
  1884. macb_or_gem_writel(bp, HRB, -1);
  1885. macb_or_gem_writel(bp, HRT, -1);
  1886. cfg |= MACB_BIT(NCFGR_MTI);
  1887. } else if (!netdev_mc_empty(dev)) {
  1888. /* Enable specific multicasts */
  1889. macb_sethashtable(dev);
  1890. cfg |= MACB_BIT(NCFGR_MTI);
  1891. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1892. /* Disable all multicast mode */
  1893. macb_or_gem_writel(bp, HRB, 0);
  1894. macb_or_gem_writel(bp, HRT, 0);
  1895. cfg &= ~MACB_BIT(NCFGR_MTI);
  1896. }
  1897. macb_writel(bp, NCFGR, cfg);
  1898. }
  1899. static int macb_open(struct net_device *dev)
  1900. {
  1901. struct macb *bp = netdev_priv(dev);
  1902. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1903. struct macb_queue *queue;
  1904. unsigned int q;
  1905. int err;
  1906. netdev_dbg(bp->dev, "open\n");
  1907. /* carrier starts down */
  1908. netif_carrier_off(dev);
  1909. /* if the phy is not yet register, retry later*/
  1910. if (!dev->phydev)
  1911. return -EAGAIN;
  1912. /* RX buffers initialization */
  1913. macb_init_rx_buffer_size(bp, bufsz);
  1914. err = macb_alloc_consistent(bp);
  1915. if (err) {
  1916. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1917. err);
  1918. return err;
  1919. }
  1920. bp->macbgem_ops.mog_init_rings(bp);
  1921. macb_init_hw(bp);
  1922. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1923. napi_enable(&queue->napi);
  1924. /* schedule a link state check */
  1925. phy_start(dev->phydev);
  1926. netif_tx_start_all_queues(dev);
  1927. if (bp->ptp_info)
  1928. bp->ptp_info->ptp_init(dev);
  1929. return 0;
  1930. }
  1931. static int macb_close(struct net_device *dev)
  1932. {
  1933. struct macb *bp = netdev_priv(dev);
  1934. struct macb_queue *queue;
  1935. unsigned long flags;
  1936. unsigned int q;
  1937. netif_tx_stop_all_queues(dev);
  1938. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1939. napi_disable(&queue->napi);
  1940. if (dev->phydev)
  1941. phy_stop(dev->phydev);
  1942. spin_lock_irqsave(&bp->lock, flags);
  1943. macb_reset_hw(bp);
  1944. netif_carrier_off(dev);
  1945. spin_unlock_irqrestore(&bp->lock, flags);
  1946. macb_free_consistent(bp);
  1947. if (bp->ptp_info)
  1948. bp->ptp_info->ptp_remove(dev);
  1949. return 0;
  1950. }
  1951. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  1952. {
  1953. if (netif_running(dev))
  1954. return -EBUSY;
  1955. dev->mtu = new_mtu;
  1956. return 0;
  1957. }
  1958. static void gem_update_stats(struct macb *bp)
  1959. {
  1960. struct macb_queue *queue;
  1961. unsigned int i, q, idx;
  1962. unsigned long *stat;
  1963. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1964. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  1965. u32 offset = gem_statistics[i].offset;
  1966. u64 val = bp->macb_reg_readl(bp, offset);
  1967. bp->ethtool_stats[i] += val;
  1968. *p += val;
  1969. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  1970. /* Add GEM_OCTTXH, GEM_OCTRXH */
  1971. val = bp->macb_reg_readl(bp, offset + 4);
  1972. bp->ethtool_stats[i] += ((u64)val) << 32;
  1973. *(++p) += val;
  1974. }
  1975. }
  1976. idx = GEM_STATS_LEN;
  1977. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1978. for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
  1979. bp->ethtool_stats[idx++] = *stat;
  1980. }
  1981. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1982. {
  1983. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1984. struct net_device_stats *nstat = &bp->dev->stats;
  1985. gem_update_stats(bp);
  1986. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1987. hwstat->rx_alignment_errors +
  1988. hwstat->rx_resource_errors +
  1989. hwstat->rx_overruns +
  1990. hwstat->rx_oversize_frames +
  1991. hwstat->rx_jabbers +
  1992. hwstat->rx_undersized_frames +
  1993. hwstat->rx_length_field_frame_errors);
  1994. nstat->tx_errors = (hwstat->tx_late_collisions +
  1995. hwstat->tx_excessive_collisions +
  1996. hwstat->tx_underrun +
  1997. hwstat->tx_carrier_sense_errors);
  1998. nstat->multicast = hwstat->rx_multicast_frames;
  1999. nstat->collisions = (hwstat->tx_single_collision_frames +
  2000. hwstat->tx_multiple_collision_frames +
  2001. hwstat->tx_excessive_collisions);
  2002. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  2003. hwstat->rx_jabbers +
  2004. hwstat->rx_undersized_frames +
  2005. hwstat->rx_length_field_frame_errors);
  2006. nstat->rx_over_errors = hwstat->rx_resource_errors;
  2007. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  2008. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  2009. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2010. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  2011. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  2012. nstat->tx_fifo_errors = hwstat->tx_underrun;
  2013. return nstat;
  2014. }
  2015. static void gem_get_ethtool_stats(struct net_device *dev,
  2016. struct ethtool_stats *stats, u64 *data)
  2017. {
  2018. struct macb *bp;
  2019. bp = netdev_priv(dev);
  2020. gem_update_stats(bp);
  2021. memcpy(data, &bp->ethtool_stats, sizeof(u64)
  2022. * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
  2023. }
  2024. static int gem_get_sset_count(struct net_device *dev, int sset)
  2025. {
  2026. struct macb *bp = netdev_priv(dev);
  2027. switch (sset) {
  2028. case ETH_SS_STATS:
  2029. return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
  2030. default:
  2031. return -EOPNOTSUPP;
  2032. }
  2033. }
  2034. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  2035. {
  2036. char stat_string[ETH_GSTRING_LEN];
  2037. struct macb *bp = netdev_priv(dev);
  2038. struct macb_queue *queue;
  2039. unsigned int i;
  2040. unsigned int q;
  2041. switch (sset) {
  2042. case ETH_SS_STATS:
  2043. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  2044. memcpy(p, gem_statistics[i].stat_string,
  2045. ETH_GSTRING_LEN);
  2046. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2047. for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
  2048. snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
  2049. q, queue_statistics[i].stat_string);
  2050. memcpy(p, stat_string, ETH_GSTRING_LEN);
  2051. }
  2052. }
  2053. break;
  2054. }
  2055. }
  2056. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  2057. {
  2058. struct macb *bp = netdev_priv(dev);
  2059. struct net_device_stats *nstat = &bp->dev->stats;
  2060. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2061. if (macb_is_gem(bp))
  2062. return gem_get_stats(bp);
  2063. /* read stats from hardware */
  2064. macb_update_stats(bp);
  2065. /* Convert HW stats into netdevice stats */
  2066. nstat->rx_errors = (hwstat->rx_fcs_errors +
  2067. hwstat->rx_align_errors +
  2068. hwstat->rx_resource_errors +
  2069. hwstat->rx_overruns +
  2070. hwstat->rx_oversize_pkts +
  2071. hwstat->rx_jabbers +
  2072. hwstat->rx_undersize_pkts +
  2073. hwstat->rx_length_mismatch);
  2074. nstat->tx_errors = (hwstat->tx_late_cols +
  2075. hwstat->tx_excessive_cols +
  2076. hwstat->tx_underruns +
  2077. hwstat->tx_carrier_errors +
  2078. hwstat->sqe_test_errors);
  2079. nstat->collisions = (hwstat->tx_single_cols +
  2080. hwstat->tx_multiple_cols +
  2081. hwstat->tx_excessive_cols);
  2082. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  2083. hwstat->rx_jabbers +
  2084. hwstat->rx_undersize_pkts +
  2085. hwstat->rx_length_mismatch);
  2086. nstat->rx_over_errors = hwstat->rx_resource_errors +
  2087. hwstat->rx_overruns;
  2088. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  2089. nstat->rx_frame_errors = hwstat->rx_align_errors;
  2090. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2091. /* XXX: What does "missed" mean? */
  2092. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  2093. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  2094. nstat->tx_fifo_errors = hwstat->tx_underruns;
  2095. /* Don't know about heartbeat or window errors... */
  2096. return nstat;
  2097. }
  2098. static int macb_get_regs_len(struct net_device *netdev)
  2099. {
  2100. return MACB_GREGS_NBR * sizeof(u32);
  2101. }
  2102. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2103. void *p)
  2104. {
  2105. struct macb *bp = netdev_priv(dev);
  2106. unsigned int tail, head;
  2107. u32 *regs_buff = p;
  2108. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  2109. | MACB_GREGS_VERSION;
  2110. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  2111. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  2112. regs_buff[0] = macb_readl(bp, NCR);
  2113. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  2114. regs_buff[2] = macb_readl(bp, NSR);
  2115. regs_buff[3] = macb_readl(bp, TSR);
  2116. regs_buff[4] = macb_readl(bp, RBQP);
  2117. regs_buff[5] = macb_readl(bp, TBQP);
  2118. regs_buff[6] = macb_readl(bp, RSR);
  2119. regs_buff[7] = macb_readl(bp, IMR);
  2120. regs_buff[8] = tail;
  2121. regs_buff[9] = head;
  2122. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  2123. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  2124. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  2125. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  2126. if (macb_is_gem(bp))
  2127. regs_buff[13] = gem_readl(bp, DMACFG);
  2128. }
  2129. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2130. {
  2131. struct macb *bp = netdev_priv(netdev);
  2132. wol->supported = 0;
  2133. wol->wolopts = 0;
  2134. if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
  2135. wol->supported = WAKE_MAGIC;
  2136. if (bp->wol & MACB_WOL_ENABLED)
  2137. wol->wolopts |= WAKE_MAGIC;
  2138. }
  2139. }
  2140. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2141. {
  2142. struct macb *bp = netdev_priv(netdev);
  2143. if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
  2144. (wol->wolopts & ~WAKE_MAGIC))
  2145. return -EOPNOTSUPP;
  2146. if (wol->wolopts & WAKE_MAGIC)
  2147. bp->wol |= MACB_WOL_ENABLED;
  2148. else
  2149. bp->wol &= ~MACB_WOL_ENABLED;
  2150. device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
  2151. return 0;
  2152. }
  2153. static void macb_get_ringparam(struct net_device *netdev,
  2154. struct ethtool_ringparam *ring)
  2155. {
  2156. struct macb *bp = netdev_priv(netdev);
  2157. ring->rx_max_pending = MAX_RX_RING_SIZE;
  2158. ring->tx_max_pending = MAX_TX_RING_SIZE;
  2159. ring->rx_pending = bp->rx_ring_size;
  2160. ring->tx_pending = bp->tx_ring_size;
  2161. }
  2162. static int macb_set_ringparam(struct net_device *netdev,
  2163. struct ethtool_ringparam *ring)
  2164. {
  2165. struct macb *bp = netdev_priv(netdev);
  2166. u32 new_rx_size, new_tx_size;
  2167. unsigned int reset = 0;
  2168. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2169. return -EINVAL;
  2170. new_rx_size = clamp_t(u32, ring->rx_pending,
  2171. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  2172. new_rx_size = roundup_pow_of_two(new_rx_size);
  2173. new_tx_size = clamp_t(u32, ring->tx_pending,
  2174. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  2175. new_tx_size = roundup_pow_of_two(new_tx_size);
  2176. if ((new_tx_size == bp->tx_ring_size) &&
  2177. (new_rx_size == bp->rx_ring_size)) {
  2178. /* nothing to do */
  2179. return 0;
  2180. }
  2181. if (netif_running(bp->dev)) {
  2182. reset = 1;
  2183. macb_close(bp->dev);
  2184. }
  2185. bp->rx_ring_size = new_rx_size;
  2186. bp->tx_ring_size = new_tx_size;
  2187. if (reset)
  2188. macb_open(bp->dev);
  2189. return 0;
  2190. }
  2191. #ifdef CONFIG_MACB_USE_HWSTAMP
  2192. static unsigned int gem_get_tsu_rate(struct macb *bp)
  2193. {
  2194. struct clk *tsu_clk;
  2195. unsigned int tsu_rate;
  2196. tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
  2197. if (!IS_ERR(tsu_clk))
  2198. tsu_rate = clk_get_rate(tsu_clk);
  2199. /* try pclk instead */
  2200. else if (!IS_ERR(bp->pclk)) {
  2201. tsu_clk = bp->pclk;
  2202. tsu_rate = clk_get_rate(tsu_clk);
  2203. } else
  2204. return -ENOTSUPP;
  2205. return tsu_rate;
  2206. }
  2207. static s32 gem_get_ptp_max_adj(void)
  2208. {
  2209. return 64000000;
  2210. }
  2211. static int gem_get_ts_info(struct net_device *dev,
  2212. struct ethtool_ts_info *info)
  2213. {
  2214. struct macb *bp = netdev_priv(dev);
  2215. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
  2216. ethtool_op_get_ts_info(dev, info);
  2217. return 0;
  2218. }
  2219. info->so_timestamping =
  2220. SOF_TIMESTAMPING_TX_SOFTWARE |
  2221. SOF_TIMESTAMPING_RX_SOFTWARE |
  2222. SOF_TIMESTAMPING_SOFTWARE |
  2223. SOF_TIMESTAMPING_TX_HARDWARE |
  2224. SOF_TIMESTAMPING_RX_HARDWARE |
  2225. SOF_TIMESTAMPING_RAW_HARDWARE;
  2226. info->tx_types =
  2227. (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
  2228. (1 << HWTSTAMP_TX_OFF) |
  2229. (1 << HWTSTAMP_TX_ON);
  2230. info->rx_filters =
  2231. (1 << HWTSTAMP_FILTER_NONE) |
  2232. (1 << HWTSTAMP_FILTER_ALL);
  2233. info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
  2234. return 0;
  2235. }
  2236. static struct macb_ptp_info gem_ptp_info = {
  2237. .ptp_init = gem_ptp_init,
  2238. .ptp_remove = gem_ptp_remove,
  2239. .get_ptp_max_adj = gem_get_ptp_max_adj,
  2240. .get_tsu_rate = gem_get_tsu_rate,
  2241. .get_ts_info = gem_get_ts_info,
  2242. .get_hwtst = gem_get_hwtst,
  2243. .set_hwtst = gem_set_hwtst,
  2244. };
  2245. #endif
  2246. static int macb_get_ts_info(struct net_device *netdev,
  2247. struct ethtool_ts_info *info)
  2248. {
  2249. struct macb *bp = netdev_priv(netdev);
  2250. if (bp->ptp_info)
  2251. return bp->ptp_info->get_ts_info(netdev, info);
  2252. return ethtool_op_get_ts_info(netdev, info);
  2253. }
  2254. static void gem_enable_flow_filters(struct macb *bp, bool enable)
  2255. {
  2256. struct ethtool_rx_fs_item *item;
  2257. u32 t2_scr;
  2258. int num_t2_scr;
  2259. num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
  2260. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2261. struct ethtool_rx_flow_spec *fs = &item->fs;
  2262. struct ethtool_tcpip4_spec *tp4sp_m;
  2263. if (fs->location >= num_t2_scr)
  2264. continue;
  2265. t2_scr = gem_readl_n(bp, SCRT2, fs->location);
  2266. /* enable/disable screener regs for the flow entry */
  2267. t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
  2268. /* only enable fields with no masking */
  2269. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2270. if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
  2271. t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
  2272. else
  2273. t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
  2274. if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
  2275. t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
  2276. else
  2277. t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
  2278. if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
  2279. t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
  2280. else
  2281. t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
  2282. gem_writel_n(bp, SCRT2, fs->location, t2_scr);
  2283. }
  2284. }
  2285. static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
  2286. {
  2287. struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
  2288. uint16_t index = fs->location;
  2289. u32 w0, w1, t2_scr;
  2290. bool cmp_a = false;
  2291. bool cmp_b = false;
  2292. bool cmp_c = false;
  2293. tp4sp_v = &(fs->h_u.tcp_ip4_spec);
  2294. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2295. /* ignore field if any masking set */
  2296. if (tp4sp_m->ip4src == 0xFFFFFFFF) {
  2297. /* 1st compare reg - IP source address */
  2298. w0 = 0;
  2299. w1 = 0;
  2300. w0 = tp4sp_v->ip4src;
  2301. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2302. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2303. w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
  2304. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
  2305. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
  2306. cmp_a = true;
  2307. }
  2308. /* ignore field if any masking set */
  2309. if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
  2310. /* 2nd compare reg - IP destination address */
  2311. w0 = 0;
  2312. w1 = 0;
  2313. w0 = tp4sp_v->ip4dst;
  2314. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2315. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2316. w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
  2317. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
  2318. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
  2319. cmp_b = true;
  2320. }
  2321. /* ignore both port fields if masking set in both */
  2322. if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
  2323. /* 3rd compare reg - source port, destination port */
  2324. w0 = 0;
  2325. w1 = 0;
  2326. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
  2327. if (tp4sp_m->psrc == tp4sp_m->pdst) {
  2328. w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
  2329. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2330. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2331. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2332. } else {
  2333. /* only one port definition */
  2334. w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
  2335. w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
  2336. if (tp4sp_m->psrc == 0xFFFF) { /* src port */
  2337. w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
  2338. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2339. } else { /* dst port */
  2340. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2341. w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
  2342. }
  2343. }
  2344. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
  2345. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
  2346. cmp_c = true;
  2347. }
  2348. t2_scr = 0;
  2349. t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
  2350. t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
  2351. if (cmp_a)
  2352. t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
  2353. if (cmp_b)
  2354. t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
  2355. if (cmp_c)
  2356. t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
  2357. gem_writel_n(bp, SCRT2, index, t2_scr);
  2358. }
  2359. static int gem_add_flow_filter(struct net_device *netdev,
  2360. struct ethtool_rxnfc *cmd)
  2361. {
  2362. struct macb *bp = netdev_priv(netdev);
  2363. struct ethtool_rx_flow_spec *fs = &cmd->fs;
  2364. struct ethtool_rx_fs_item *item, *newfs;
  2365. unsigned long flags;
  2366. int ret = -EINVAL;
  2367. bool added = false;
  2368. newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
  2369. if (newfs == NULL)
  2370. return -ENOMEM;
  2371. memcpy(&newfs->fs, fs, sizeof(newfs->fs));
  2372. netdev_dbg(netdev,
  2373. "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2374. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2375. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2376. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2377. htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
  2378. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2379. /* find correct place to add in list */
  2380. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2381. if (item->fs.location > newfs->fs.location) {
  2382. list_add_tail(&newfs->list, &item->list);
  2383. added = true;
  2384. break;
  2385. } else if (item->fs.location == fs->location) {
  2386. netdev_err(netdev, "Rule not added: location %d not free!\n",
  2387. fs->location);
  2388. ret = -EBUSY;
  2389. goto err;
  2390. }
  2391. }
  2392. if (!added)
  2393. list_add_tail(&newfs->list, &bp->rx_fs_list.list);
  2394. gem_prog_cmp_regs(bp, fs);
  2395. bp->rx_fs_list.count++;
  2396. /* enable filtering if NTUPLE on */
  2397. if (netdev->features & NETIF_F_NTUPLE)
  2398. gem_enable_flow_filters(bp, 1);
  2399. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2400. return 0;
  2401. err:
  2402. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2403. kfree(newfs);
  2404. return ret;
  2405. }
  2406. static int gem_del_flow_filter(struct net_device *netdev,
  2407. struct ethtool_rxnfc *cmd)
  2408. {
  2409. struct macb *bp = netdev_priv(netdev);
  2410. struct ethtool_rx_fs_item *item;
  2411. struct ethtool_rx_flow_spec *fs;
  2412. unsigned long flags;
  2413. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2414. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2415. if (item->fs.location == cmd->fs.location) {
  2416. /* disable screener regs for the flow entry */
  2417. fs = &(item->fs);
  2418. netdev_dbg(netdev,
  2419. "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2420. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2421. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2422. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2423. htons(fs->h_u.tcp_ip4_spec.psrc),
  2424. htons(fs->h_u.tcp_ip4_spec.pdst));
  2425. gem_writel_n(bp, SCRT2, fs->location, 0);
  2426. list_del(&item->list);
  2427. bp->rx_fs_list.count--;
  2428. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2429. kfree(item);
  2430. return 0;
  2431. }
  2432. }
  2433. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2434. return -EINVAL;
  2435. }
  2436. static int gem_get_flow_entry(struct net_device *netdev,
  2437. struct ethtool_rxnfc *cmd)
  2438. {
  2439. struct macb *bp = netdev_priv(netdev);
  2440. struct ethtool_rx_fs_item *item;
  2441. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2442. if (item->fs.location == cmd->fs.location) {
  2443. memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
  2444. return 0;
  2445. }
  2446. }
  2447. return -EINVAL;
  2448. }
  2449. static int gem_get_all_flow_entries(struct net_device *netdev,
  2450. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  2451. {
  2452. struct macb *bp = netdev_priv(netdev);
  2453. struct ethtool_rx_fs_item *item;
  2454. uint32_t cnt = 0;
  2455. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2456. if (cnt == cmd->rule_cnt)
  2457. return -EMSGSIZE;
  2458. rule_locs[cnt] = item->fs.location;
  2459. cnt++;
  2460. }
  2461. cmd->data = bp->max_tuples;
  2462. cmd->rule_cnt = cnt;
  2463. return 0;
  2464. }
  2465. static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  2466. u32 *rule_locs)
  2467. {
  2468. struct macb *bp = netdev_priv(netdev);
  2469. int ret = 0;
  2470. switch (cmd->cmd) {
  2471. case ETHTOOL_GRXRINGS:
  2472. cmd->data = bp->num_queues;
  2473. break;
  2474. case ETHTOOL_GRXCLSRLCNT:
  2475. cmd->rule_cnt = bp->rx_fs_list.count;
  2476. break;
  2477. case ETHTOOL_GRXCLSRULE:
  2478. ret = gem_get_flow_entry(netdev, cmd);
  2479. break;
  2480. case ETHTOOL_GRXCLSRLALL:
  2481. ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
  2482. break;
  2483. default:
  2484. netdev_err(netdev,
  2485. "Command parameter %d is not supported\n", cmd->cmd);
  2486. ret = -EOPNOTSUPP;
  2487. }
  2488. return ret;
  2489. }
  2490. static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  2491. {
  2492. struct macb *bp = netdev_priv(netdev);
  2493. int ret;
  2494. switch (cmd->cmd) {
  2495. case ETHTOOL_SRXCLSRLINS:
  2496. if ((cmd->fs.location >= bp->max_tuples)
  2497. || (cmd->fs.ring_cookie >= bp->num_queues)) {
  2498. ret = -EINVAL;
  2499. break;
  2500. }
  2501. ret = gem_add_flow_filter(netdev, cmd);
  2502. break;
  2503. case ETHTOOL_SRXCLSRLDEL:
  2504. ret = gem_del_flow_filter(netdev, cmd);
  2505. break;
  2506. default:
  2507. netdev_err(netdev,
  2508. "Command parameter %d is not supported\n", cmd->cmd);
  2509. ret = -EOPNOTSUPP;
  2510. }
  2511. return ret;
  2512. }
  2513. static const struct ethtool_ops macb_ethtool_ops = {
  2514. .get_regs_len = macb_get_regs_len,
  2515. .get_regs = macb_get_regs,
  2516. .get_link = ethtool_op_get_link,
  2517. .get_ts_info = ethtool_op_get_ts_info,
  2518. .get_wol = macb_get_wol,
  2519. .set_wol = macb_set_wol,
  2520. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2521. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2522. .get_ringparam = macb_get_ringparam,
  2523. .set_ringparam = macb_set_ringparam,
  2524. };
  2525. static const struct ethtool_ops gem_ethtool_ops = {
  2526. .get_regs_len = macb_get_regs_len,
  2527. .get_regs = macb_get_regs,
  2528. .get_link = ethtool_op_get_link,
  2529. .get_ts_info = macb_get_ts_info,
  2530. .get_ethtool_stats = gem_get_ethtool_stats,
  2531. .get_strings = gem_get_ethtool_strings,
  2532. .get_sset_count = gem_get_sset_count,
  2533. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2534. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2535. .get_ringparam = macb_get_ringparam,
  2536. .set_ringparam = macb_set_ringparam,
  2537. .get_rxnfc = gem_get_rxnfc,
  2538. .set_rxnfc = gem_set_rxnfc,
  2539. };
  2540. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2541. {
  2542. struct phy_device *phydev = dev->phydev;
  2543. struct macb *bp = netdev_priv(dev);
  2544. if (!netif_running(dev))
  2545. return -EINVAL;
  2546. if (!phydev)
  2547. return -ENODEV;
  2548. if (!bp->ptp_info)
  2549. return phy_mii_ioctl(phydev, rq, cmd);
  2550. switch (cmd) {
  2551. case SIOCSHWTSTAMP:
  2552. return bp->ptp_info->set_hwtst(dev, rq, cmd);
  2553. case SIOCGHWTSTAMP:
  2554. return bp->ptp_info->get_hwtst(dev, rq);
  2555. default:
  2556. return phy_mii_ioctl(phydev, rq, cmd);
  2557. }
  2558. }
  2559. static int macb_set_features(struct net_device *netdev,
  2560. netdev_features_t features)
  2561. {
  2562. struct macb *bp = netdev_priv(netdev);
  2563. netdev_features_t changed = features ^ netdev->features;
  2564. /* TX checksum offload */
  2565. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  2566. u32 dmacfg;
  2567. dmacfg = gem_readl(bp, DMACFG);
  2568. if (features & NETIF_F_HW_CSUM)
  2569. dmacfg |= GEM_BIT(TXCOEN);
  2570. else
  2571. dmacfg &= ~GEM_BIT(TXCOEN);
  2572. gem_writel(bp, DMACFG, dmacfg);
  2573. }
  2574. /* RX checksum offload */
  2575. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  2576. u32 netcfg;
  2577. netcfg = gem_readl(bp, NCFGR);
  2578. if (features & NETIF_F_RXCSUM &&
  2579. !(netdev->flags & IFF_PROMISC))
  2580. netcfg |= GEM_BIT(RXCOEN);
  2581. else
  2582. netcfg &= ~GEM_BIT(RXCOEN);
  2583. gem_writel(bp, NCFGR, netcfg);
  2584. }
  2585. /* RX Flow Filters */
  2586. if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
  2587. bool turn_on = features & NETIF_F_NTUPLE;
  2588. gem_enable_flow_filters(bp, turn_on);
  2589. }
  2590. return 0;
  2591. }
  2592. static const struct net_device_ops macb_netdev_ops = {
  2593. .ndo_open = macb_open,
  2594. .ndo_stop = macb_close,
  2595. .ndo_start_xmit = macb_start_xmit,
  2596. .ndo_set_rx_mode = macb_set_rx_mode,
  2597. .ndo_get_stats = macb_get_stats,
  2598. .ndo_do_ioctl = macb_ioctl,
  2599. .ndo_validate_addr = eth_validate_addr,
  2600. .ndo_change_mtu = macb_change_mtu,
  2601. .ndo_set_mac_address = eth_mac_addr,
  2602. #ifdef CONFIG_NET_POLL_CONTROLLER
  2603. .ndo_poll_controller = macb_poll_controller,
  2604. #endif
  2605. .ndo_set_features = macb_set_features,
  2606. .ndo_features_check = macb_features_check,
  2607. };
  2608. /* Configure peripheral capabilities according to device tree
  2609. * and integration options used
  2610. */
  2611. static void macb_configure_caps(struct macb *bp,
  2612. const struct macb_config *dt_conf)
  2613. {
  2614. u32 dcfg;
  2615. if (dt_conf)
  2616. bp->caps = dt_conf->caps;
  2617. if (hw_is_gem(bp->regs, bp->native_io)) {
  2618. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  2619. dcfg = gem_readl(bp, DCFG1);
  2620. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  2621. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  2622. dcfg = gem_readl(bp, DCFG2);
  2623. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  2624. bp->caps |= MACB_CAPS_FIFO_MODE;
  2625. #ifdef CONFIG_MACB_USE_HWSTAMP
  2626. if (gem_has_ptp(bp)) {
  2627. if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
  2628. pr_err("GEM doesn't support hardware ptp.\n");
  2629. else {
  2630. bp->hw_dma_cap |= HW_DMA_CAP_PTP;
  2631. bp->ptp_info = &gem_ptp_info;
  2632. }
  2633. }
  2634. #endif
  2635. }
  2636. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  2637. }
  2638. static void macb_probe_queues(void __iomem *mem,
  2639. bool native_io,
  2640. unsigned int *queue_mask,
  2641. unsigned int *num_queues)
  2642. {
  2643. unsigned int hw_q;
  2644. *queue_mask = 0x1;
  2645. *num_queues = 1;
  2646. /* is it macb or gem ?
  2647. *
  2648. * We need to read directly from the hardware here because
  2649. * we are early in the probe process and don't have the
  2650. * MACB_CAPS_MACB_IS_GEM flag positioned
  2651. */
  2652. if (!hw_is_gem(mem, native_io))
  2653. return;
  2654. /* bit 0 is never set but queue 0 always exists */
  2655. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  2656. *queue_mask |= 0x1;
  2657. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  2658. if (*queue_mask & (1 << hw_q))
  2659. (*num_queues)++;
  2660. }
  2661. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  2662. struct clk **hclk, struct clk **tx_clk,
  2663. struct clk **rx_clk)
  2664. {
  2665. struct macb_platform_data *pdata;
  2666. int err;
  2667. pdata = dev_get_platdata(&pdev->dev);
  2668. if (pdata) {
  2669. *pclk = pdata->pclk;
  2670. *hclk = pdata->hclk;
  2671. } else {
  2672. *pclk = devm_clk_get(&pdev->dev, "pclk");
  2673. *hclk = devm_clk_get(&pdev->dev, "hclk");
  2674. }
  2675. if (IS_ERR(*pclk)) {
  2676. err = PTR_ERR(*pclk);
  2677. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  2678. return err;
  2679. }
  2680. if (IS_ERR(*hclk)) {
  2681. err = PTR_ERR(*hclk);
  2682. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  2683. return err;
  2684. }
  2685. *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  2686. if (IS_ERR(*tx_clk))
  2687. *tx_clk = NULL;
  2688. *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
  2689. if (IS_ERR(*rx_clk))
  2690. *rx_clk = NULL;
  2691. err = clk_prepare_enable(*pclk);
  2692. if (err) {
  2693. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2694. return err;
  2695. }
  2696. err = clk_prepare_enable(*hclk);
  2697. if (err) {
  2698. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  2699. goto err_disable_pclk;
  2700. }
  2701. err = clk_prepare_enable(*tx_clk);
  2702. if (err) {
  2703. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  2704. goto err_disable_hclk;
  2705. }
  2706. err = clk_prepare_enable(*rx_clk);
  2707. if (err) {
  2708. dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
  2709. goto err_disable_txclk;
  2710. }
  2711. return 0;
  2712. err_disable_txclk:
  2713. clk_disable_unprepare(*tx_clk);
  2714. err_disable_hclk:
  2715. clk_disable_unprepare(*hclk);
  2716. err_disable_pclk:
  2717. clk_disable_unprepare(*pclk);
  2718. return err;
  2719. }
  2720. static int macb_init(struct platform_device *pdev)
  2721. {
  2722. struct net_device *dev = platform_get_drvdata(pdev);
  2723. unsigned int hw_q, q;
  2724. struct macb *bp = netdev_priv(dev);
  2725. struct macb_queue *queue;
  2726. int err;
  2727. u32 val, reg;
  2728. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  2729. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  2730. /* set the queue register mapping once for all: queue0 has a special
  2731. * register mapping but we don't want to test the queue index then
  2732. * compute the corresponding register offset at run time.
  2733. */
  2734. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  2735. if (!(bp->queue_mask & (1 << hw_q)))
  2736. continue;
  2737. queue = &bp->queues[q];
  2738. queue->bp = bp;
  2739. netif_napi_add(dev, &queue->napi, macb_poll, 64);
  2740. if (hw_q) {
  2741. queue->ISR = GEM_ISR(hw_q - 1);
  2742. queue->IER = GEM_IER(hw_q - 1);
  2743. queue->IDR = GEM_IDR(hw_q - 1);
  2744. queue->IMR = GEM_IMR(hw_q - 1);
  2745. queue->TBQP = GEM_TBQP(hw_q - 1);
  2746. queue->RBQP = GEM_RBQP(hw_q - 1);
  2747. queue->RBQS = GEM_RBQS(hw_q - 1);
  2748. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2749. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2750. queue->TBQPH = GEM_TBQPH(hw_q - 1);
  2751. queue->RBQPH = GEM_RBQPH(hw_q - 1);
  2752. }
  2753. #endif
  2754. } else {
  2755. /* queue0 uses legacy registers */
  2756. queue->ISR = MACB_ISR;
  2757. queue->IER = MACB_IER;
  2758. queue->IDR = MACB_IDR;
  2759. queue->IMR = MACB_IMR;
  2760. queue->TBQP = MACB_TBQP;
  2761. queue->RBQP = MACB_RBQP;
  2762. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2763. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2764. queue->TBQPH = MACB_TBQPH;
  2765. queue->RBQPH = MACB_RBQPH;
  2766. }
  2767. #endif
  2768. }
  2769. /* get irq: here we use the linux queue index, not the hardware
  2770. * queue index. the queue irq definitions in the device tree
  2771. * must remove the optional gaps that could exist in the
  2772. * hardware queue mask.
  2773. */
  2774. queue->irq = platform_get_irq(pdev, q);
  2775. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  2776. IRQF_SHARED, dev->name, queue);
  2777. if (err) {
  2778. dev_err(&pdev->dev,
  2779. "Unable to request IRQ %d (error %d)\n",
  2780. queue->irq, err);
  2781. return err;
  2782. }
  2783. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  2784. q++;
  2785. }
  2786. dev->netdev_ops = &macb_netdev_ops;
  2787. /* setup appropriated routines according to adapter type */
  2788. if (macb_is_gem(bp)) {
  2789. bp->max_tx_length = GEM_MAX_TX_LEN;
  2790. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  2791. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  2792. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  2793. bp->macbgem_ops.mog_rx = gem_rx;
  2794. dev->ethtool_ops = &gem_ethtool_ops;
  2795. } else {
  2796. bp->max_tx_length = MACB_MAX_TX_LEN;
  2797. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  2798. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  2799. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  2800. bp->macbgem_ops.mog_rx = macb_rx;
  2801. dev->ethtool_ops = &macb_ethtool_ops;
  2802. }
  2803. /* Set features */
  2804. dev->hw_features = NETIF_F_SG;
  2805. /* Check LSO capability */
  2806. if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  2807. dev->hw_features |= MACB_NETIF_LSO;
  2808. /* Checksum offload is only available on gem with packet buffer */
  2809. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  2810. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  2811. if (bp->caps & MACB_CAPS_SG_DISABLED)
  2812. dev->hw_features &= ~NETIF_F_SG;
  2813. dev->features = dev->hw_features;
  2814. /* Check RX Flow Filters support.
  2815. * Max Rx flows set by availability of screeners & compare regs:
  2816. * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
  2817. */
  2818. reg = gem_readl(bp, DCFG8);
  2819. bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
  2820. GEM_BFEXT(T2SCR, reg));
  2821. if (bp->max_tuples > 0) {
  2822. /* also needs one ethtype match to check IPv4 */
  2823. if (GEM_BFEXT(SCR2ETH, reg) > 0) {
  2824. /* program this reg now */
  2825. reg = 0;
  2826. reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
  2827. gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
  2828. /* Filtering is supported in hw but don't enable it in kernel now */
  2829. dev->hw_features |= NETIF_F_NTUPLE;
  2830. /* init Rx flow definitions */
  2831. INIT_LIST_HEAD(&bp->rx_fs_list.list);
  2832. bp->rx_fs_list.count = 0;
  2833. spin_lock_init(&bp->rx_fs_lock);
  2834. } else
  2835. bp->max_tuples = 0;
  2836. }
  2837. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  2838. val = 0;
  2839. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  2840. val = GEM_BIT(RGMII);
  2841. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  2842. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2843. val = MACB_BIT(RMII);
  2844. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2845. val = MACB_BIT(MII);
  2846. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  2847. val |= MACB_BIT(CLKEN);
  2848. macb_or_gem_writel(bp, USRIO, val);
  2849. }
  2850. /* Set MII management clock divider */
  2851. val = macb_mdc_clk_div(bp);
  2852. val |= macb_dbw(bp);
  2853. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2854. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  2855. macb_writel(bp, NCFGR, val);
  2856. return 0;
  2857. }
  2858. #if defined(CONFIG_OF)
  2859. /* 1518 rounded up */
  2860. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  2861. /* max number of receive buffers */
  2862. #define AT91ETHER_MAX_RX_DESCR 9
  2863. /* Initialize and start the Receiver and Transmit subsystems */
  2864. static int at91ether_start(struct net_device *dev)
  2865. {
  2866. struct macb *lp = netdev_priv(dev);
  2867. struct macb_queue *q = &lp->queues[0];
  2868. struct macb_dma_desc *desc;
  2869. dma_addr_t addr;
  2870. u32 ctl;
  2871. int i;
  2872. q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  2873. (AT91ETHER_MAX_RX_DESCR *
  2874. macb_dma_desc_get_size(lp)),
  2875. &q->rx_ring_dma, GFP_KERNEL);
  2876. if (!q->rx_ring)
  2877. return -ENOMEM;
  2878. q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  2879. AT91ETHER_MAX_RX_DESCR *
  2880. AT91ETHER_MAX_RBUFF_SZ,
  2881. &q->rx_buffers_dma, GFP_KERNEL);
  2882. if (!q->rx_buffers) {
  2883. dma_free_coherent(&lp->pdev->dev,
  2884. AT91ETHER_MAX_RX_DESCR *
  2885. macb_dma_desc_get_size(lp),
  2886. q->rx_ring, q->rx_ring_dma);
  2887. q->rx_ring = NULL;
  2888. return -ENOMEM;
  2889. }
  2890. addr = q->rx_buffers_dma;
  2891. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  2892. desc = macb_rx_desc(q, i);
  2893. macb_set_addr(lp, desc, addr);
  2894. desc->ctrl = 0;
  2895. addr += AT91ETHER_MAX_RBUFF_SZ;
  2896. }
  2897. /* Set the Wrap bit on the last descriptor */
  2898. desc->addr |= MACB_BIT(RX_WRAP);
  2899. /* Reset buffer index */
  2900. q->rx_tail = 0;
  2901. /* Program address of descriptor list in Rx Buffer Queue register */
  2902. macb_writel(lp, RBQP, q->rx_ring_dma);
  2903. /* Enable Receive and Transmit */
  2904. ctl = macb_readl(lp, NCR);
  2905. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  2906. return 0;
  2907. }
  2908. /* Open the ethernet interface */
  2909. static int at91ether_open(struct net_device *dev)
  2910. {
  2911. struct macb *lp = netdev_priv(dev);
  2912. u32 ctl;
  2913. int ret;
  2914. /* Clear internal statistics */
  2915. ctl = macb_readl(lp, NCR);
  2916. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  2917. macb_set_hwaddr(lp);
  2918. ret = at91ether_start(dev);
  2919. if (ret)
  2920. return ret;
  2921. /* Enable MAC interrupts */
  2922. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  2923. MACB_BIT(RXUBR) |
  2924. MACB_BIT(ISR_TUND) |
  2925. MACB_BIT(ISR_RLE) |
  2926. MACB_BIT(TCOMP) |
  2927. MACB_BIT(ISR_ROVR) |
  2928. MACB_BIT(HRESP));
  2929. /* schedule a link state check */
  2930. phy_start(dev->phydev);
  2931. netif_start_queue(dev);
  2932. return 0;
  2933. }
  2934. /* Close the interface */
  2935. static int at91ether_close(struct net_device *dev)
  2936. {
  2937. struct macb *lp = netdev_priv(dev);
  2938. struct macb_queue *q = &lp->queues[0];
  2939. u32 ctl;
  2940. /* Disable Receiver and Transmitter */
  2941. ctl = macb_readl(lp, NCR);
  2942. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  2943. /* Disable MAC interrupts */
  2944. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  2945. MACB_BIT(RXUBR) |
  2946. MACB_BIT(ISR_TUND) |
  2947. MACB_BIT(ISR_RLE) |
  2948. MACB_BIT(TCOMP) |
  2949. MACB_BIT(ISR_ROVR) |
  2950. MACB_BIT(HRESP));
  2951. netif_stop_queue(dev);
  2952. dma_free_coherent(&lp->pdev->dev,
  2953. AT91ETHER_MAX_RX_DESCR *
  2954. macb_dma_desc_get_size(lp),
  2955. q->rx_ring, q->rx_ring_dma);
  2956. q->rx_ring = NULL;
  2957. dma_free_coherent(&lp->pdev->dev,
  2958. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  2959. q->rx_buffers, q->rx_buffers_dma);
  2960. q->rx_buffers = NULL;
  2961. return 0;
  2962. }
  2963. /* Transmit packet */
  2964. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2965. {
  2966. struct macb *lp = netdev_priv(dev);
  2967. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  2968. netif_stop_queue(dev);
  2969. /* Store packet information (to free when Tx completed) */
  2970. lp->skb = skb;
  2971. lp->skb_length = skb->len;
  2972. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  2973. DMA_TO_DEVICE);
  2974. if (dma_mapping_error(NULL, lp->skb_physaddr)) {
  2975. dev_kfree_skb_any(skb);
  2976. dev->stats.tx_dropped++;
  2977. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  2978. return NETDEV_TX_OK;
  2979. }
  2980. /* Set address of the data in the Transmit Address register */
  2981. macb_writel(lp, TAR, lp->skb_physaddr);
  2982. /* Set length of the packet in the Transmit Control register */
  2983. macb_writel(lp, TCR, skb->len);
  2984. } else {
  2985. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  2986. return NETDEV_TX_BUSY;
  2987. }
  2988. return NETDEV_TX_OK;
  2989. }
  2990. /* Extract received frame from buffer descriptors and sent to upper layers.
  2991. * (Called from interrupt context)
  2992. */
  2993. static void at91ether_rx(struct net_device *dev)
  2994. {
  2995. struct macb *lp = netdev_priv(dev);
  2996. struct macb_queue *q = &lp->queues[0];
  2997. struct macb_dma_desc *desc;
  2998. unsigned char *p_recv;
  2999. struct sk_buff *skb;
  3000. unsigned int pktlen;
  3001. desc = macb_rx_desc(q, q->rx_tail);
  3002. while (desc->addr & MACB_BIT(RX_USED)) {
  3003. p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  3004. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  3005. skb = netdev_alloc_skb(dev, pktlen + 2);
  3006. if (skb) {
  3007. skb_reserve(skb, 2);
  3008. skb_put_data(skb, p_recv, pktlen);
  3009. skb->protocol = eth_type_trans(skb, dev);
  3010. dev->stats.rx_packets++;
  3011. dev->stats.rx_bytes += pktlen;
  3012. netif_rx(skb);
  3013. } else {
  3014. dev->stats.rx_dropped++;
  3015. }
  3016. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  3017. dev->stats.multicast++;
  3018. /* reset ownership bit */
  3019. desc->addr &= ~MACB_BIT(RX_USED);
  3020. /* wrap after last buffer */
  3021. if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  3022. q->rx_tail = 0;
  3023. else
  3024. q->rx_tail++;
  3025. desc = macb_rx_desc(q, q->rx_tail);
  3026. }
  3027. }
  3028. /* MAC interrupt handler */
  3029. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  3030. {
  3031. struct net_device *dev = dev_id;
  3032. struct macb *lp = netdev_priv(dev);
  3033. u32 intstatus, ctl;
  3034. /* MAC Interrupt Status register indicates what interrupts are pending.
  3035. * It is automatically cleared once read.
  3036. */
  3037. intstatus = macb_readl(lp, ISR);
  3038. /* Receive complete */
  3039. if (intstatus & MACB_BIT(RCOMP))
  3040. at91ether_rx(dev);
  3041. /* Transmit complete */
  3042. if (intstatus & MACB_BIT(TCOMP)) {
  3043. /* The TCOM bit is set even if the transmission failed */
  3044. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  3045. dev->stats.tx_errors++;
  3046. if (lp->skb) {
  3047. dev_kfree_skb_irq(lp->skb);
  3048. lp->skb = NULL;
  3049. dma_unmap_single(NULL, lp->skb_physaddr,
  3050. lp->skb_length, DMA_TO_DEVICE);
  3051. dev->stats.tx_packets++;
  3052. dev->stats.tx_bytes += lp->skb_length;
  3053. }
  3054. netif_wake_queue(dev);
  3055. }
  3056. /* Work-around for EMAC Errata section 41.3.1 */
  3057. if (intstatus & MACB_BIT(RXUBR)) {
  3058. ctl = macb_readl(lp, NCR);
  3059. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  3060. wmb();
  3061. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  3062. }
  3063. if (intstatus & MACB_BIT(ISR_ROVR))
  3064. netdev_err(dev, "ROVR error\n");
  3065. return IRQ_HANDLED;
  3066. }
  3067. #ifdef CONFIG_NET_POLL_CONTROLLER
  3068. static void at91ether_poll_controller(struct net_device *dev)
  3069. {
  3070. unsigned long flags;
  3071. local_irq_save(flags);
  3072. at91ether_interrupt(dev->irq, dev);
  3073. local_irq_restore(flags);
  3074. }
  3075. #endif
  3076. static const struct net_device_ops at91ether_netdev_ops = {
  3077. .ndo_open = at91ether_open,
  3078. .ndo_stop = at91ether_close,
  3079. .ndo_start_xmit = at91ether_start_xmit,
  3080. .ndo_get_stats = macb_get_stats,
  3081. .ndo_set_rx_mode = macb_set_rx_mode,
  3082. .ndo_set_mac_address = eth_mac_addr,
  3083. .ndo_do_ioctl = macb_ioctl,
  3084. .ndo_validate_addr = eth_validate_addr,
  3085. #ifdef CONFIG_NET_POLL_CONTROLLER
  3086. .ndo_poll_controller = at91ether_poll_controller,
  3087. #endif
  3088. };
  3089. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  3090. struct clk **hclk, struct clk **tx_clk,
  3091. struct clk **rx_clk)
  3092. {
  3093. int err;
  3094. *hclk = NULL;
  3095. *tx_clk = NULL;
  3096. *rx_clk = NULL;
  3097. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  3098. if (IS_ERR(*pclk))
  3099. return PTR_ERR(*pclk);
  3100. err = clk_prepare_enable(*pclk);
  3101. if (err) {
  3102. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  3103. return err;
  3104. }
  3105. return 0;
  3106. }
  3107. static int at91ether_init(struct platform_device *pdev)
  3108. {
  3109. struct net_device *dev = platform_get_drvdata(pdev);
  3110. struct macb *bp = netdev_priv(dev);
  3111. int err;
  3112. u32 reg;
  3113. dev->netdev_ops = &at91ether_netdev_ops;
  3114. dev->ethtool_ops = &macb_ethtool_ops;
  3115. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  3116. 0, dev->name, dev);
  3117. if (err)
  3118. return err;
  3119. macb_writel(bp, NCR, 0);
  3120. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  3121. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  3122. reg |= MACB_BIT(RM9200_RMII);
  3123. macb_writel(bp, NCFGR, reg);
  3124. return 0;
  3125. }
  3126. static const struct macb_config at91sam9260_config = {
  3127. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3128. .clk_init = macb_clk_init,
  3129. .init = macb_init,
  3130. };
  3131. static const struct macb_config pc302gem_config = {
  3132. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  3133. .dma_burst_length = 16,
  3134. .clk_init = macb_clk_init,
  3135. .init = macb_init,
  3136. };
  3137. static const struct macb_config sama5d2_config = {
  3138. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3139. .dma_burst_length = 16,
  3140. .clk_init = macb_clk_init,
  3141. .init = macb_init,
  3142. };
  3143. static const struct macb_config sama5d3_config = {
  3144. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
  3145. | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  3146. .dma_burst_length = 16,
  3147. .clk_init = macb_clk_init,
  3148. .init = macb_init,
  3149. .jumbo_max_len = 10240,
  3150. };
  3151. static const struct macb_config sama5d4_config = {
  3152. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3153. .dma_burst_length = 4,
  3154. .clk_init = macb_clk_init,
  3155. .init = macb_init,
  3156. };
  3157. static const struct macb_config emac_config = {
  3158. .clk_init = at91ether_clk_init,
  3159. .init = at91ether_init,
  3160. };
  3161. static const struct macb_config np4_config = {
  3162. .caps = MACB_CAPS_USRIO_DISABLED,
  3163. .clk_init = macb_clk_init,
  3164. .init = macb_init,
  3165. };
  3166. static const struct macb_config zynqmp_config = {
  3167. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3168. MACB_CAPS_JUMBO |
  3169. MACB_CAPS_GEM_HAS_PTP,
  3170. .dma_burst_length = 16,
  3171. .clk_init = macb_clk_init,
  3172. .init = macb_init,
  3173. .jumbo_max_len = 10240,
  3174. };
  3175. static const struct macb_config zynq_config = {
  3176. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  3177. .dma_burst_length = 16,
  3178. .clk_init = macb_clk_init,
  3179. .init = macb_init,
  3180. };
  3181. static const struct of_device_id macb_dt_ids[] = {
  3182. { .compatible = "cdns,at32ap7000-macb" },
  3183. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  3184. { .compatible = "cdns,macb" },
  3185. { .compatible = "cdns,np4-macb", .data = &np4_config },
  3186. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  3187. { .compatible = "cdns,gem", .data = &pc302gem_config },
  3188. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  3189. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  3190. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  3191. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  3192. { .compatible = "cdns,emac", .data = &emac_config },
  3193. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  3194. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  3195. { /* sentinel */ }
  3196. };
  3197. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  3198. #endif /* CONFIG_OF */
  3199. static const struct macb_config default_gem_config = {
  3200. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3201. MACB_CAPS_JUMBO |
  3202. MACB_CAPS_GEM_HAS_PTP,
  3203. .dma_burst_length = 16,
  3204. .clk_init = macb_clk_init,
  3205. .init = macb_init,
  3206. .jumbo_max_len = 10240,
  3207. };
  3208. static int macb_probe(struct platform_device *pdev)
  3209. {
  3210. const struct macb_config *macb_config = &default_gem_config;
  3211. int (*clk_init)(struct platform_device *, struct clk **,
  3212. struct clk **, struct clk **, struct clk **)
  3213. = macb_config->clk_init;
  3214. int (*init)(struct platform_device *) = macb_config->init;
  3215. struct device_node *np = pdev->dev.of_node;
  3216. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  3217. unsigned int queue_mask, num_queues;
  3218. struct macb_platform_data *pdata;
  3219. bool native_io;
  3220. struct phy_device *phydev;
  3221. struct net_device *dev;
  3222. struct resource *regs;
  3223. void __iomem *mem;
  3224. const char *mac;
  3225. struct macb *bp;
  3226. int err;
  3227. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3228. mem = devm_ioremap_resource(&pdev->dev, regs);
  3229. if (IS_ERR(mem))
  3230. return PTR_ERR(mem);
  3231. if (np) {
  3232. const struct of_device_id *match;
  3233. match = of_match_node(macb_dt_ids, np);
  3234. if (match && match->data) {
  3235. macb_config = match->data;
  3236. clk_init = macb_config->clk_init;
  3237. init = macb_config->init;
  3238. }
  3239. }
  3240. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
  3241. if (err)
  3242. return err;
  3243. native_io = hw_is_native_io(mem);
  3244. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  3245. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  3246. if (!dev) {
  3247. err = -ENOMEM;
  3248. goto err_disable_clocks;
  3249. }
  3250. dev->base_addr = regs->start;
  3251. SET_NETDEV_DEV(dev, &pdev->dev);
  3252. bp = netdev_priv(dev);
  3253. bp->pdev = pdev;
  3254. bp->dev = dev;
  3255. bp->regs = mem;
  3256. bp->native_io = native_io;
  3257. if (native_io) {
  3258. bp->macb_reg_readl = hw_readl_native;
  3259. bp->macb_reg_writel = hw_writel_native;
  3260. } else {
  3261. bp->macb_reg_readl = hw_readl;
  3262. bp->macb_reg_writel = hw_writel;
  3263. }
  3264. bp->num_queues = num_queues;
  3265. bp->queue_mask = queue_mask;
  3266. if (macb_config)
  3267. bp->dma_burst_length = macb_config->dma_burst_length;
  3268. bp->pclk = pclk;
  3269. bp->hclk = hclk;
  3270. bp->tx_clk = tx_clk;
  3271. bp->rx_clk = rx_clk;
  3272. if (macb_config)
  3273. bp->jumbo_max_len = macb_config->jumbo_max_len;
  3274. bp->wol = 0;
  3275. if (of_get_property(np, "magic-packet", NULL))
  3276. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  3277. device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  3278. spin_lock_init(&bp->lock);
  3279. /* setup capabilities */
  3280. macb_configure_caps(bp, macb_config);
  3281. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3282. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  3283. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  3284. bp->hw_dma_cap |= HW_DMA_CAP_64B;
  3285. }
  3286. #endif
  3287. platform_set_drvdata(pdev, dev);
  3288. dev->irq = platform_get_irq(pdev, 0);
  3289. if (dev->irq < 0) {
  3290. err = dev->irq;
  3291. goto err_out_free_netdev;
  3292. }
  3293. /* MTU range: 68 - 1500 or 10240 */
  3294. dev->min_mtu = GEM_MTU_MIN_SIZE;
  3295. if (bp->caps & MACB_CAPS_JUMBO)
  3296. dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  3297. else
  3298. dev->max_mtu = ETH_DATA_LEN;
  3299. mac = of_get_mac_address(np);
  3300. if (mac) {
  3301. ether_addr_copy(bp->dev->dev_addr, mac);
  3302. } else {
  3303. err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
  3304. if (err) {
  3305. if (err == -EPROBE_DEFER)
  3306. goto err_out_free_netdev;
  3307. macb_get_hwaddr(bp);
  3308. }
  3309. }
  3310. err = of_get_phy_mode(np);
  3311. if (err < 0) {
  3312. pdata = dev_get_platdata(&pdev->dev);
  3313. if (pdata && pdata->is_rmii)
  3314. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  3315. else
  3316. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  3317. } else {
  3318. bp->phy_interface = err;
  3319. }
  3320. /* IP specific init */
  3321. err = init(pdev);
  3322. if (err)
  3323. goto err_out_free_netdev;
  3324. err = macb_mii_init(bp);
  3325. if (err)
  3326. goto err_out_free_netdev;
  3327. phydev = dev->phydev;
  3328. netif_carrier_off(dev);
  3329. err = register_netdev(dev);
  3330. if (err) {
  3331. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  3332. goto err_out_unregister_mdio;
  3333. }
  3334. tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
  3335. (unsigned long)bp);
  3336. phy_attached_info(phydev);
  3337. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  3338. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  3339. dev->base_addr, dev->irq, dev->dev_addr);
  3340. return 0;
  3341. err_out_unregister_mdio:
  3342. phy_disconnect(dev->phydev);
  3343. mdiobus_unregister(bp->mii_bus);
  3344. of_node_put(bp->phy_node);
  3345. if (np && of_phy_is_fixed_link(np))
  3346. of_phy_deregister_fixed_link(np);
  3347. mdiobus_free(bp->mii_bus);
  3348. err_out_free_netdev:
  3349. free_netdev(dev);
  3350. err_disable_clocks:
  3351. clk_disable_unprepare(tx_clk);
  3352. clk_disable_unprepare(hclk);
  3353. clk_disable_unprepare(pclk);
  3354. clk_disable_unprepare(rx_clk);
  3355. return err;
  3356. }
  3357. static int macb_remove(struct platform_device *pdev)
  3358. {
  3359. struct net_device *dev;
  3360. struct macb *bp;
  3361. struct device_node *np = pdev->dev.of_node;
  3362. dev = platform_get_drvdata(pdev);
  3363. if (dev) {
  3364. bp = netdev_priv(dev);
  3365. if (dev->phydev)
  3366. phy_disconnect(dev->phydev);
  3367. mdiobus_unregister(bp->mii_bus);
  3368. if (np && of_phy_is_fixed_link(np))
  3369. of_phy_deregister_fixed_link(np);
  3370. dev->phydev = NULL;
  3371. mdiobus_free(bp->mii_bus);
  3372. unregister_netdev(dev);
  3373. clk_disable_unprepare(bp->tx_clk);
  3374. clk_disable_unprepare(bp->hclk);
  3375. clk_disable_unprepare(bp->pclk);
  3376. clk_disable_unprepare(bp->rx_clk);
  3377. of_node_put(bp->phy_node);
  3378. free_netdev(dev);
  3379. }
  3380. return 0;
  3381. }
  3382. static int __maybe_unused macb_suspend(struct device *dev)
  3383. {
  3384. struct platform_device *pdev = to_platform_device(dev);
  3385. struct net_device *netdev = platform_get_drvdata(pdev);
  3386. struct macb *bp = netdev_priv(netdev);
  3387. netif_carrier_off(netdev);
  3388. netif_device_detach(netdev);
  3389. if (bp->wol & MACB_WOL_ENABLED) {
  3390. macb_writel(bp, IER, MACB_BIT(WOL));
  3391. macb_writel(bp, WOL, MACB_BIT(MAG));
  3392. enable_irq_wake(bp->queues[0].irq);
  3393. } else {
  3394. clk_disable_unprepare(bp->tx_clk);
  3395. clk_disable_unprepare(bp->hclk);
  3396. clk_disable_unprepare(bp->pclk);
  3397. clk_disable_unprepare(bp->rx_clk);
  3398. }
  3399. return 0;
  3400. }
  3401. static int __maybe_unused macb_resume(struct device *dev)
  3402. {
  3403. struct platform_device *pdev = to_platform_device(dev);
  3404. struct net_device *netdev = platform_get_drvdata(pdev);
  3405. struct macb *bp = netdev_priv(netdev);
  3406. if (bp->wol & MACB_WOL_ENABLED) {
  3407. macb_writel(bp, IDR, MACB_BIT(WOL));
  3408. macb_writel(bp, WOL, 0);
  3409. disable_irq_wake(bp->queues[0].irq);
  3410. } else {
  3411. clk_prepare_enable(bp->pclk);
  3412. clk_prepare_enable(bp->hclk);
  3413. clk_prepare_enable(bp->tx_clk);
  3414. clk_prepare_enable(bp->rx_clk);
  3415. }
  3416. netif_device_attach(netdev);
  3417. return 0;
  3418. }
  3419. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  3420. static struct platform_driver macb_driver = {
  3421. .probe = macb_probe,
  3422. .remove = macb_remove,
  3423. .driver = {
  3424. .name = "macb",
  3425. .of_match_table = of_match_ptr(macb_dt_ids),
  3426. .pm = &macb_pm_ops,
  3427. },
  3428. };
  3429. module_platform_driver(macb_driver);
  3430. MODULE_LICENSE("GPL");
  3431. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  3432. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  3433. MODULE_ALIAS("platform:macb");