macb_main.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115
  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. bp->queues[0].bp = bp;
  3114. dev->netdev_ops = &at91ether_netdev_ops;
  3115. dev->ethtool_ops = &macb_ethtool_ops;
  3116. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  3117. 0, dev->name, dev);
  3118. if (err)
  3119. return err;
  3120. macb_writel(bp, NCR, 0);
  3121. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  3122. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  3123. reg |= MACB_BIT(RM9200_RMII);
  3124. macb_writel(bp, NCFGR, reg);
  3125. return 0;
  3126. }
  3127. static const struct macb_config at91sam9260_config = {
  3128. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3129. .clk_init = macb_clk_init,
  3130. .init = macb_init,
  3131. };
  3132. static const struct macb_config pc302gem_config = {
  3133. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  3134. .dma_burst_length = 16,
  3135. .clk_init = macb_clk_init,
  3136. .init = macb_init,
  3137. };
  3138. static const struct macb_config sama5d2_config = {
  3139. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3140. .dma_burst_length = 16,
  3141. .clk_init = macb_clk_init,
  3142. .init = macb_init,
  3143. };
  3144. static const struct macb_config sama5d3_config = {
  3145. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
  3146. | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  3147. .dma_burst_length = 16,
  3148. .clk_init = macb_clk_init,
  3149. .init = macb_init,
  3150. .jumbo_max_len = 10240,
  3151. };
  3152. static const struct macb_config sama5d4_config = {
  3153. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3154. .dma_burst_length = 4,
  3155. .clk_init = macb_clk_init,
  3156. .init = macb_init,
  3157. };
  3158. static const struct macb_config emac_config = {
  3159. .clk_init = at91ether_clk_init,
  3160. .init = at91ether_init,
  3161. };
  3162. static const struct macb_config np4_config = {
  3163. .caps = MACB_CAPS_USRIO_DISABLED,
  3164. .clk_init = macb_clk_init,
  3165. .init = macb_init,
  3166. };
  3167. static const struct macb_config zynqmp_config = {
  3168. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3169. MACB_CAPS_JUMBO |
  3170. MACB_CAPS_GEM_HAS_PTP,
  3171. .dma_burst_length = 16,
  3172. .clk_init = macb_clk_init,
  3173. .init = macb_init,
  3174. .jumbo_max_len = 10240,
  3175. };
  3176. static const struct macb_config zynq_config = {
  3177. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  3178. .dma_burst_length = 16,
  3179. .clk_init = macb_clk_init,
  3180. .init = macb_init,
  3181. };
  3182. static const struct of_device_id macb_dt_ids[] = {
  3183. { .compatible = "cdns,at32ap7000-macb" },
  3184. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  3185. { .compatible = "cdns,macb" },
  3186. { .compatible = "cdns,np4-macb", .data = &np4_config },
  3187. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  3188. { .compatible = "cdns,gem", .data = &pc302gem_config },
  3189. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  3190. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  3191. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  3192. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  3193. { .compatible = "cdns,emac", .data = &emac_config },
  3194. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  3195. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  3196. { /* sentinel */ }
  3197. };
  3198. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  3199. #endif /* CONFIG_OF */
  3200. static const struct macb_config default_gem_config = {
  3201. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3202. MACB_CAPS_JUMBO |
  3203. MACB_CAPS_GEM_HAS_PTP,
  3204. .dma_burst_length = 16,
  3205. .clk_init = macb_clk_init,
  3206. .init = macb_init,
  3207. .jumbo_max_len = 10240,
  3208. };
  3209. static int macb_probe(struct platform_device *pdev)
  3210. {
  3211. const struct macb_config *macb_config = &default_gem_config;
  3212. int (*clk_init)(struct platform_device *, struct clk **,
  3213. struct clk **, struct clk **, struct clk **)
  3214. = macb_config->clk_init;
  3215. int (*init)(struct platform_device *) = macb_config->init;
  3216. struct device_node *np = pdev->dev.of_node;
  3217. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  3218. unsigned int queue_mask, num_queues;
  3219. struct macb_platform_data *pdata;
  3220. bool native_io;
  3221. struct phy_device *phydev;
  3222. struct net_device *dev;
  3223. struct resource *regs;
  3224. void __iomem *mem;
  3225. const char *mac;
  3226. struct macb *bp;
  3227. int err;
  3228. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3229. mem = devm_ioremap_resource(&pdev->dev, regs);
  3230. if (IS_ERR(mem))
  3231. return PTR_ERR(mem);
  3232. if (np) {
  3233. const struct of_device_id *match;
  3234. match = of_match_node(macb_dt_ids, np);
  3235. if (match && match->data) {
  3236. macb_config = match->data;
  3237. clk_init = macb_config->clk_init;
  3238. init = macb_config->init;
  3239. }
  3240. }
  3241. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
  3242. if (err)
  3243. return err;
  3244. native_io = hw_is_native_io(mem);
  3245. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  3246. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  3247. if (!dev) {
  3248. err = -ENOMEM;
  3249. goto err_disable_clocks;
  3250. }
  3251. dev->base_addr = regs->start;
  3252. SET_NETDEV_DEV(dev, &pdev->dev);
  3253. bp = netdev_priv(dev);
  3254. bp->pdev = pdev;
  3255. bp->dev = dev;
  3256. bp->regs = mem;
  3257. bp->native_io = native_io;
  3258. if (native_io) {
  3259. bp->macb_reg_readl = hw_readl_native;
  3260. bp->macb_reg_writel = hw_writel_native;
  3261. } else {
  3262. bp->macb_reg_readl = hw_readl;
  3263. bp->macb_reg_writel = hw_writel;
  3264. }
  3265. bp->num_queues = num_queues;
  3266. bp->queue_mask = queue_mask;
  3267. if (macb_config)
  3268. bp->dma_burst_length = macb_config->dma_burst_length;
  3269. bp->pclk = pclk;
  3270. bp->hclk = hclk;
  3271. bp->tx_clk = tx_clk;
  3272. bp->rx_clk = rx_clk;
  3273. if (macb_config)
  3274. bp->jumbo_max_len = macb_config->jumbo_max_len;
  3275. bp->wol = 0;
  3276. if (of_get_property(np, "magic-packet", NULL))
  3277. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  3278. device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  3279. spin_lock_init(&bp->lock);
  3280. /* setup capabilities */
  3281. macb_configure_caps(bp, macb_config);
  3282. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3283. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  3284. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  3285. bp->hw_dma_cap |= HW_DMA_CAP_64B;
  3286. }
  3287. #endif
  3288. platform_set_drvdata(pdev, dev);
  3289. dev->irq = platform_get_irq(pdev, 0);
  3290. if (dev->irq < 0) {
  3291. err = dev->irq;
  3292. goto err_out_free_netdev;
  3293. }
  3294. /* MTU range: 68 - 1500 or 10240 */
  3295. dev->min_mtu = GEM_MTU_MIN_SIZE;
  3296. if (bp->caps & MACB_CAPS_JUMBO)
  3297. dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  3298. else
  3299. dev->max_mtu = ETH_DATA_LEN;
  3300. mac = of_get_mac_address(np);
  3301. if (mac) {
  3302. ether_addr_copy(bp->dev->dev_addr, mac);
  3303. } else {
  3304. err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
  3305. if (err) {
  3306. if (err == -EPROBE_DEFER)
  3307. goto err_out_free_netdev;
  3308. macb_get_hwaddr(bp);
  3309. }
  3310. }
  3311. err = of_get_phy_mode(np);
  3312. if (err < 0) {
  3313. pdata = dev_get_platdata(&pdev->dev);
  3314. if (pdata && pdata->is_rmii)
  3315. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  3316. else
  3317. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  3318. } else {
  3319. bp->phy_interface = err;
  3320. }
  3321. /* IP specific init */
  3322. err = init(pdev);
  3323. if (err)
  3324. goto err_out_free_netdev;
  3325. err = macb_mii_init(bp);
  3326. if (err)
  3327. goto err_out_free_netdev;
  3328. phydev = dev->phydev;
  3329. netif_carrier_off(dev);
  3330. err = register_netdev(dev);
  3331. if (err) {
  3332. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  3333. goto err_out_unregister_mdio;
  3334. }
  3335. tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
  3336. (unsigned long)bp);
  3337. phy_attached_info(phydev);
  3338. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  3339. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  3340. dev->base_addr, dev->irq, dev->dev_addr);
  3341. return 0;
  3342. err_out_unregister_mdio:
  3343. phy_disconnect(dev->phydev);
  3344. mdiobus_unregister(bp->mii_bus);
  3345. of_node_put(bp->phy_node);
  3346. if (np && of_phy_is_fixed_link(np))
  3347. of_phy_deregister_fixed_link(np);
  3348. mdiobus_free(bp->mii_bus);
  3349. err_out_free_netdev:
  3350. free_netdev(dev);
  3351. err_disable_clocks:
  3352. clk_disable_unprepare(tx_clk);
  3353. clk_disable_unprepare(hclk);
  3354. clk_disable_unprepare(pclk);
  3355. clk_disable_unprepare(rx_clk);
  3356. return err;
  3357. }
  3358. static int macb_remove(struct platform_device *pdev)
  3359. {
  3360. struct net_device *dev;
  3361. struct macb *bp;
  3362. struct device_node *np = pdev->dev.of_node;
  3363. dev = platform_get_drvdata(pdev);
  3364. if (dev) {
  3365. bp = netdev_priv(dev);
  3366. if (dev->phydev)
  3367. phy_disconnect(dev->phydev);
  3368. mdiobus_unregister(bp->mii_bus);
  3369. if (np && of_phy_is_fixed_link(np))
  3370. of_phy_deregister_fixed_link(np);
  3371. dev->phydev = NULL;
  3372. mdiobus_free(bp->mii_bus);
  3373. unregister_netdev(dev);
  3374. clk_disable_unprepare(bp->tx_clk);
  3375. clk_disable_unprepare(bp->hclk);
  3376. clk_disable_unprepare(bp->pclk);
  3377. clk_disable_unprepare(bp->rx_clk);
  3378. of_node_put(bp->phy_node);
  3379. free_netdev(dev);
  3380. }
  3381. return 0;
  3382. }
  3383. static int __maybe_unused macb_suspend(struct device *dev)
  3384. {
  3385. struct platform_device *pdev = to_platform_device(dev);
  3386. struct net_device *netdev = platform_get_drvdata(pdev);
  3387. struct macb *bp = netdev_priv(netdev);
  3388. netif_carrier_off(netdev);
  3389. netif_device_detach(netdev);
  3390. if (bp->wol & MACB_WOL_ENABLED) {
  3391. macb_writel(bp, IER, MACB_BIT(WOL));
  3392. macb_writel(bp, WOL, MACB_BIT(MAG));
  3393. enable_irq_wake(bp->queues[0].irq);
  3394. } else {
  3395. clk_disable_unprepare(bp->tx_clk);
  3396. clk_disable_unprepare(bp->hclk);
  3397. clk_disable_unprepare(bp->pclk);
  3398. clk_disable_unprepare(bp->rx_clk);
  3399. }
  3400. return 0;
  3401. }
  3402. static int __maybe_unused macb_resume(struct device *dev)
  3403. {
  3404. struct platform_device *pdev = to_platform_device(dev);
  3405. struct net_device *netdev = platform_get_drvdata(pdev);
  3406. struct macb *bp = netdev_priv(netdev);
  3407. if (bp->wol & MACB_WOL_ENABLED) {
  3408. macb_writel(bp, IDR, MACB_BIT(WOL));
  3409. macb_writel(bp, WOL, 0);
  3410. disable_irq_wake(bp->queues[0].irq);
  3411. } else {
  3412. clk_prepare_enable(bp->pclk);
  3413. clk_prepare_enable(bp->hclk);
  3414. clk_prepare_enable(bp->tx_clk);
  3415. clk_prepare_enable(bp->rx_clk);
  3416. }
  3417. netif_device_attach(netdev);
  3418. return 0;
  3419. }
  3420. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  3421. static struct platform_driver macb_driver = {
  3422. .probe = macb_probe,
  3423. .remove = macb_remove,
  3424. .driver = {
  3425. .name = "macb",
  3426. .of_match_table = of_match_ptr(macb_dt_ids),
  3427. .pm = &macb_pm_ops,
  3428. },
  3429. };
  3430. module_platform_driver(macb_driver);
  3431. MODULE_LICENSE("GPL");
  3432. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  3433. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  3434. MODULE_ALIAS("platform:macb");