macb_main.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129
  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. int size;
  1505. bp->macbgem_ops.mog_free_rx_buffers(bp);
  1506. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1507. kfree(queue->tx_skb);
  1508. queue->tx_skb = NULL;
  1509. if (queue->tx_ring) {
  1510. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  1511. dma_free_coherent(&bp->pdev->dev, size,
  1512. queue->tx_ring, queue->tx_ring_dma);
  1513. queue->tx_ring = NULL;
  1514. }
  1515. if (queue->rx_ring) {
  1516. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  1517. dma_free_coherent(&bp->pdev->dev, size,
  1518. queue->rx_ring, queue->rx_ring_dma);
  1519. queue->rx_ring = NULL;
  1520. }
  1521. }
  1522. }
  1523. static int gem_alloc_rx_buffers(struct macb *bp)
  1524. {
  1525. struct macb_queue *queue;
  1526. unsigned int q;
  1527. int size;
  1528. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1529. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  1530. queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
  1531. if (!queue->rx_skbuff)
  1532. return -ENOMEM;
  1533. else
  1534. netdev_dbg(bp->dev,
  1535. "Allocated %d RX struct sk_buff entries at %p\n",
  1536. bp->rx_ring_size, queue->rx_skbuff);
  1537. }
  1538. return 0;
  1539. }
  1540. static int macb_alloc_rx_buffers(struct macb *bp)
  1541. {
  1542. struct macb_queue *queue = &bp->queues[0];
  1543. int size;
  1544. size = bp->rx_ring_size * bp->rx_buffer_size;
  1545. queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  1546. &queue->rx_buffers_dma, GFP_KERNEL);
  1547. if (!queue->rx_buffers)
  1548. return -ENOMEM;
  1549. netdev_dbg(bp->dev,
  1550. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  1551. size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
  1552. return 0;
  1553. }
  1554. static int macb_alloc_consistent(struct macb *bp)
  1555. {
  1556. struct macb_queue *queue;
  1557. unsigned int q;
  1558. int size;
  1559. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1560. size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
  1561. queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1562. &queue->tx_ring_dma,
  1563. GFP_KERNEL);
  1564. if (!queue->tx_ring)
  1565. goto out_err;
  1566. netdev_dbg(bp->dev,
  1567. "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
  1568. q, size, (unsigned long)queue->tx_ring_dma,
  1569. queue->tx_ring);
  1570. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  1571. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  1572. if (!queue->tx_skb)
  1573. goto out_err;
  1574. size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
  1575. queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
  1576. &queue->rx_ring_dma, GFP_KERNEL);
  1577. if (!queue->rx_ring)
  1578. goto out_err;
  1579. netdev_dbg(bp->dev,
  1580. "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
  1581. size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
  1582. }
  1583. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  1584. goto out_err;
  1585. return 0;
  1586. out_err:
  1587. macb_free_consistent(bp);
  1588. return -ENOMEM;
  1589. }
  1590. static void gem_init_rings(struct macb *bp)
  1591. {
  1592. struct macb_queue *queue;
  1593. struct macb_dma_desc *desc = NULL;
  1594. unsigned int q;
  1595. int i;
  1596. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1597. for (i = 0; i < bp->tx_ring_size; i++) {
  1598. desc = macb_tx_desc(queue, i);
  1599. macb_set_addr(bp, desc, 0);
  1600. desc->ctrl = MACB_BIT(TX_USED);
  1601. }
  1602. desc->ctrl |= MACB_BIT(TX_WRAP);
  1603. queue->tx_head = 0;
  1604. queue->tx_tail = 0;
  1605. queue->rx_tail = 0;
  1606. queue->rx_prepared_head = 0;
  1607. gem_rx_refill(queue);
  1608. }
  1609. }
  1610. static void macb_init_rings(struct macb *bp)
  1611. {
  1612. int i;
  1613. struct macb_dma_desc *desc = NULL;
  1614. macb_init_rx_ring(&bp->queues[0]);
  1615. for (i = 0; i < bp->tx_ring_size; i++) {
  1616. desc = macb_tx_desc(&bp->queues[0], i);
  1617. macb_set_addr(bp, desc, 0);
  1618. desc->ctrl = MACB_BIT(TX_USED);
  1619. }
  1620. bp->queues[0].tx_head = 0;
  1621. bp->queues[0].tx_tail = 0;
  1622. desc->ctrl |= MACB_BIT(TX_WRAP);
  1623. }
  1624. static void macb_reset_hw(struct macb *bp)
  1625. {
  1626. struct macb_queue *queue;
  1627. unsigned int q;
  1628. /* Disable RX and TX (XXX: Should we halt the transmission
  1629. * more gracefully?)
  1630. */
  1631. macb_writel(bp, NCR, 0);
  1632. /* Clear the stats registers (XXX: Update stats first?) */
  1633. macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
  1634. /* Clear all status flags */
  1635. macb_writel(bp, TSR, -1);
  1636. macb_writel(bp, RSR, -1);
  1637. /* Disable all interrupts */
  1638. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1639. queue_writel(queue, IDR, -1);
  1640. queue_readl(queue, ISR);
  1641. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1642. queue_writel(queue, ISR, -1);
  1643. }
  1644. }
  1645. static u32 gem_mdc_clk_div(struct macb *bp)
  1646. {
  1647. u32 config;
  1648. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  1649. if (pclk_hz <= 20000000)
  1650. config = GEM_BF(CLK, GEM_CLK_DIV8);
  1651. else if (pclk_hz <= 40000000)
  1652. config = GEM_BF(CLK, GEM_CLK_DIV16);
  1653. else if (pclk_hz <= 80000000)
  1654. config = GEM_BF(CLK, GEM_CLK_DIV32);
  1655. else if (pclk_hz <= 120000000)
  1656. config = GEM_BF(CLK, GEM_CLK_DIV48);
  1657. else if (pclk_hz <= 160000000)
  1658. config = GEM_BF(CLK, GEM_CLK_DIV64);
  1659. else
  1660. config = GEM_BF(CLK, GEM_CLK_DIV96);
  1661. return config;
  1662. }
  1663. static u32 macb_mdc_clk_div(struct macb *bp)
  1664. {
  1665. u32 config;
  1666. unsigned long pclk_hz;
  1667. if (macb_is_gem(bp))
  1668. return gem_mdc_clk_div(bp);
  1669. pclk_hz = clk_get_rate(bp->pclk);
  1670. if (pclk_hz <= 20000000)
  1671. config = MACB_BF(CLK, MACB_CLK_DIV8);
  1672. else if (pclk_hz <= 40000000)
  1673. config = MACB_BF(CLK, MACB_CLK_DIV16);
  1674. else if (pclk_hz <= 80000000)
  1675. config = MACB_BF(CLK, MACB_CLK_DIV32);
  1676. else
  1677. config = MACB_BF(CLK, MACB_CLK_DIV64);
  1678. return config;
  1679. }
  1680. /* Get the DMA bus width field of the network configuration register that we
  1681. * should program. We find the width from decoding the design configuration
  1682. * register to find the maximum supported data bus width.
  1683. */
  1684. static u32 macb_dbw(struct macb *bp)
  1685. {
  1686. if (!macb_is_gem(bp))
  1687. return 0;
  1688. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  1689. case 4:
  1690. return GEM_BF(DBW, GEM_DBW128);
  1691. case 2:
  1692. return GEM_BF(DBW, GEM_DBW64);
  1693. case 1:
  1694. default:
  1695. return GEM_BF(DBW, GEM_DBW32);
  1696. }
  1697. }
  1698. /* Configure the receive DMA engine
  1699. * - use the correct receive buffer size
  1700. * - set best burst length for DMA operations
  1701. * (if not supported by FIFO, it will fallback to default)
  1702. * - set both rx/tx packet buffers to full memory size
  1703. * These are configurable parameters for GEM.
  1704. */
  1705. static void macb_configure_dma(struct macb *bp)
  1706. {
  1707. struct macb_queue *queue;
  1708. u32 buffer_size;
  1709. unsigned int q;
  1710. u32 dmacfg;
  1711. buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
  1712. if (macb_is_gem(bp)) {
  1713. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  1714. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1715. if (q)
  1716. queue_writel(queue, RBQS, buffer_size);
  1717. else
  1718. dmacfg |= GEM_BF(RXBS, buffer_size);
  1719. }
  1720. if (bp->dma_burst_length)
  1721. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  1722. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  1723. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  1724. if (bp->native_io)
  1725. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  1726. else
  1727. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  1728. if (bp->dev->features & NETIF_F_HW_CSUM)
  1729. dmacfg |= GEM_BIT(TXCOEN);
  1730. else
  1731. dmacfg &= ~GEM_BIT(TXCOEN);
  1732. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1733. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1734. dmacfg |= GEM_BIT(ADDR64);
  1735. #endif
  1736. #ifdef CONFIG_MACB_USE_HWSTAMP
  1737. if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
  1738. dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
  1739. #endif
  1740. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  1741. dmacfg);
  1742. gem_writel(bp, DMACFG, dmacfg);
  1743. }
  1744. }
  1745. static void macb_init_hw(struct macb *bp)
  1746. {
  1747. struct macb_queue *queue;
  1748. unsigned int q;
  1749. u32 config;
  1750. macb_reset_hw(bp);
  1751. macb_set_hwaddr(bp);
  1752. config = macb_mdc_clk_div(bp);
  1753. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  1754. config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  1755. config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
  1756. config |= MACB_BIT(PAE); /* PAuse Enable */
  1757. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  1758. if (bp->caps & MACB_CAPS_JUMBO)
  1759. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  1760. else
  1761. config |= MACB_BIT(BIG); /* Receive oversized frames */
  1762. if (bp->dev->flags & IFF_PROMISC)
  1763. config |= MACB_BIT(CAF); /* Copy All Frames */
  1764. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  1765. config |= GEM_BIT(RXCOEN);
  1766. if (!(bp->dev->flags & IFF_BROADCAST))
  1767. config |= MACB_BIT(NBC); /* No BroadCast */
  1768. config |= macb_dbw(bp);
  1769. macb_writel(bp, NCFGR, config);
  1770. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  1771. gem_writel(bp, JML, bp->jumbo_max_len);
  1772. bp->speed = SPEED_10;
  1773. bp->duplex = DUPLEX_HALF;
  1774. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  1775. if (bp->caps & MACB_CAPS_JUMBO)
  1776. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  1777. macb_configure_dma(bp);
  1778. /* Initialize TX and RX buffers */
  1779. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1780. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  1781. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1782. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1783. queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
  1784. #endif
  1785. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  1786. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  1787. if (bp->hw_dma_cap & HW_DMA_CAP_64B)
  1788. queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
  1789. #endif
  1790. /* Enable interrupts */
  1791. queue_writel(queue, IER,
  1792. MACB_RX_INT_FLAGS |
  1793. MACB_TX_INT_FLAGS |
  1794. MACB_BIT(HRESP));
  1795. }
  1796. /* Enable TX and RX */
  1797. macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
  1798. }
  1799. /* The hash address register is 64 bits long and takes up two
  1800. * locations in the memory map. The least significant bits are stored
  1801. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  1802. *
  1803. * The unicast hash enable and the multicast hash enable bits in the
  1804. * network configuration register enable the reception of hash matched
  1805. * frames. The destination address is reduced to a 6 bit index into
  1806. * the 64 bit hash register using the following hash function. The
  1807. * hash function is an exclusive or of every sixth bit of the
  1808. * destination address.
  1809. *
  1810. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  1811. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  1812. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  1813. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  1814. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  1815. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  1816. *
  1817. * da[0] represents the least significant bit of the first byte
  1818. * received, that is, the multicast/unicast indicator, and da[47]
  1819. * represents the most significant bit of the last byte received. If
  1820. * the hash index, hi[n], points to a bit that is set in the hash
  1821. * register then the frame will be matched according to whether the
  1822. * frame is multicast or unicast. A multicast match will be signalled
  1823. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  1824. * index points to a bit set in the hash register. A unicast match
  1825. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  1826. * and the hash index points to a bit set in the hash register. To
  1827. * receive all multicast frames, the hash register should be set with
  1828. * all ones and the multicast hash enable bit should be set in the
  1829. * network configuration register.
  1830. */
  1831. static inline int hash_bit_value(int bitnr, __u8 *addr)
  1832. {
  1833. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  1834. return 1;
  1835. return 0;
  1836. }
  1837. /* Return the hash index value for the specified address. */
  1838. static int hash_get_index(__u8 *addr)
  1839. {
  1840. int i, j, bitval;
  1841. int hash_index = 0;
  1842. for (j = 0; j < 6; j++) {
  1843. for (i = 0, bitval = 0; i < 8; i++)
  1844. bitval ^= hash_bit_value(i * 6 + j, addr);
  1845. hash_index |= (bitval << j);
  1846. }
  1847. return hash_index;
  1848. }
  1849. /* Add multicast addresses to the internal multicast-hash table. */
  1850. static void macb_sethashtable(struct net_device *dev)
  1851. {
  1852. struct netdev_hw_addr *ha;
  1853. unsigned long mc_filter[2];
  1854. unsigned int bitnr;
  1855. struct macb *bp = netdev_priv(dev);
  1856. mc_filter[0] = 0;
  1857. mc_filter[1] = 0;
  1858. netdev_for_each_mc_addr(ha, dev) {
  1859. bitnr = hash_get_index(ha->addr);
  1860. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  1861. }
  1862. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  1863. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  1864. }
  1865. /* Enable/Disable promiscuous and multicast modes. */
  1866. static void macb_set_rx_mode(struct net_device *dev)
  1867. {
  1868. unsigned long cfg;
  1869. struct macb *bp = netdev_priv(dev);
  1870. cfg = macb_readl(bp, NCFGR);
  1871. if (dev->flags & IFF_PROMISC) {
  1872. /* Enable promiscuous mode */
  1873. cfg |= MACB_BIT(CAF);
  1874. /* Disable RX checksum offload */
  1875. if (macb_is_gem(bp))
  1876. cfg &= ~GEM_BIT(RXCOEN);
  1877. } else {
  1878. /* Disable promiscuous mode */
  1879. cfg &= ~MACB_BIT(CAF);
  1880. /* Enable RX checksum offload only if requested */
  1881. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  1882. cfg |= GEM_BIT(RXCOEN);
  1883. }
  1884. if (dev->flags & IFF_ALLMULTI) {
  1885. /* Enable all multicast mode */
  1886. macb_or_gem_writel(bp, HRB, -1);
  1887. macb_or_gem_writel(bp, HRT, -1);
  1888. cfg |= MACB_BIT(NCFGR_MTI);
  1889. } else if (!netdev_mc_empty(dev)) {
  1890. /* Enable specific multicasts */
  1891. macb_sethashtable(dev);
  1892. cfg |= MACB_BIT(NCFGR_MTI);
  1893. } else if (dev->flags & (~IFF_ALLMULTI)) {
  1894. /* Disable all multicast mode */
  1895. macb_or_gem_writel(bp, HRB, 0);
  1896. macb_or_gem_writel(bp, HRT, 0);
  1897. cfg &= ~MACB_BIT(NCFGR_MTI);
  1898. }
  1899. macb_writel(bp, NCFGR, cfg);
  1900. }
  1901. static int macb_open(struct net_device *dev)
  1902. {
  1903. struct macb *bp = netdev_priv(dev);
  1904. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  1905. struct macb_queue *queue;
  1906. unsigned int q;
  1907. int err;
  1908. netdev_dbg(bp->dev, "open\n");
  1909. /* carrier starts down */
  1910. netif_carrier_off(dev);
  1911. /* if the phy is not yet register, retry later*/
  1912. if (!dev->phydev)
  1913. return -EAGAIN;
  1914. /* RX buffers initialization */
  1915. macb_init_rx_buffer_size(bp, bufsz);
  1916. err = macb_alloc_consistent(bp);
  1917. if (err) {
  1918. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  1919. err);
  1920. return err;
  1921. }
  1922. bp->macbgem_ops.mog_init_rings(bp);
  1923. macb_init_hw(bp);
  1924. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1925. napi_enable(&queue->napi);
  1926. /* schedule a link state check */
  1927. phy_start(dev->phydev);
  1928. netif_tx_start_all_queues(dev);
  1929. if (bp->ptp_info)
  1930. bp->ptp_info->ptp_init(dev);
  1931. return 0;
  1932. }
  1933. static int macb_close(struct net_device *dev)
  1934. {
  1935. struct macb *bp = netdev_priv(dev);
  1936. struct macb_queue *queue;
  1937. unsigned long flags;
  1938. unsigned int q;
  1939. netif_tx_stop_all_queues(dev);
  1940. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1941. napi_disable(&queue->napi);
  1942. if (dev->phydev)
  1943. phy_stop(dev->phydev);
  1944. spin_lock_irqsave(&bp->lock, flags);
  1945. macb_reset_hw(bp);
  1946. netif_carrier_off(dev);
  1947. spin_unlock_irqrestore(&bp->lock, flags);
  1948. macb_free_consistent(bp);
  1949. if (bp->ptp_info)
  1950. bp->ptp_info->ptp_remove(dev);
  1951. return 0;
  1952. }
  1953. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  1954. {
  1955. if (netif_running(dev))
  1956. return -EBUSY;
  1957. dev->mtu = new_mtu;
  1958. return 0;
  1959. }
  1960. static void gem_update_stats(struct macb *bp)
  1961. {
  1962. struct macb_queue *queue;
  1963. unsigned int i, q, idx;
  1964. unsigned long *stat;
  1965. u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
  1966. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  1967. u32 offset = gem_statistics[i].offset;
  1968. u64 val = bp->macb_reg_readl(bp, offset);
  1969. bp->ethtool_stats[i] += val;
  1970. *p += val;
  1971. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  1972. /* Add GEM_OCTTXH, GEM_OCTRXH */
  1973. val = bp->macb_reg_readl(bp, offset + 4);
  1974. bp->ethtool_stats[i] += ((u64)val) << 32;
  1975. *(++p) += val;
  1976. }
  1977. }
  1978. idx = GEM_STATS_LEN;
  1979. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1980. for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
  1981. bp->ethtool_stats[idx++] = *stat;
  1982. }
  1983. static struct net_device_stats *gem_get_stats(struct macb *bp)
  1984. {
  1985. struct gem_stats *hwstat = &bp->hw_stats.gem;
  1986. struct net_device_stats *nstat = &bp->dev->stats;
  1987. gem_update_stats(bp);
  1988. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  1989. hwstat->rx_alignment_errors +
  1990. hwstat->rx_resource_errors +
  1991. hwstat->rx_overruns +
  1992. hwstat->rx_oversize_frames +
  1993. hwstat->rx_jabbers +
  1994. hwstat->rx_undersized_frames +
  1995. hwstat->rx_length_field_frame_errors);
  1996. nstat->tx_errors = (hwstat->tx_late_collisions +
  1997. hwstat->tx_excessive_collisions +
  1998. hwstat->tx_underrun +
  1999. hwstat->tx_carrier_sense_errors);
  2000. nstat->multicast = hwstat->rx_multicast_frames;
  2001. nstat->collisions = (hwstat->tx_single_collision_frames +
  2002. hwstat->tx_multiple_collision_frames +
  2003. hwstat->tx_excessive_collisions);
  2004. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  2005. hwstat->rx_jabbers +
  2006. hwstat->rx_undersized_frames +
  2007. hwstat->rx_length_field_frame_errors);
  2008. nstat->rx_over_errors = hwstat->rx_resource_errors;
  2009. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  2010. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  2011. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2012. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  2013. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  2014. nstat->tx_fifo_errors = hwstat->tx_underrun;
  2015. return nstat;
  2016. }
  2017. static void gem_get_ethtool_stats(struct net_device *dev,
  2018. struct ethtool_stats *stats, u64 *data)
  2019. {
  2020. struct macb *bp;
  2021. bp = netdev_priv(dev);
  2022. gem_update_stats(bp);
  2023. memcpy(data, &bp->ethtool_stats, sizeof(u64)
  2024. * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
  2025. }
  2026. static int gem_get_sset_count(struct net_device *dev, int sset)
  2027. {
  2028. struct macb *bp = netdev_priv(dev);
  2029. switch (sset) {
  2030. case ETH_SS_STATS:
  2031. return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
  2032. default:
  2033. return -EOPNOTSUPP;
  2034. }
  2035. }
  2036. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  2037. {
  2038. char stat_string[ETH_GSTRING_LEN];
  2039. struct macb *bp = netdev_priv(dev);
  2040. struct macb_queue *queue;
  2041. unsigned int i;
  2042. unsigned int q;
  2043. switch (sset) {
  2044. case ETH_SS_STATS:
  2045. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  2046. memcpy(p, gem_statistics[i].stat_string,
  2047. ETH_GSTRING_LEN);
  2048. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2049. for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
  2050. snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
  2051. q, queue_statistics[i].stat_string);
  2052. memcpy(p, stat_string, ETH_GSTRING_LEN);
  2053. }
  2054. }
  2055. break;
  2056. }
  2057. }
  2058. static struct net_device_stats *macb_get_stats(struct net_device *dev)
  2059. {
  2060. struct macb *bp = netdev_priv(dev);
  2061. struct net_device_stats *nstat = &bp->dev->stats;
  2062. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2063. if (macb_is_gem(bp))
  2064. return gem_get_stats(bp);
  2065. /* read stats from hardware */
  2066. macb_update_stats(bp);
  2067. /* Convert HW stats into netdevice stats */
  2068. nstat->rx_errors = (hwstat->rx_fcs_errors +
  2069. hwstat->rx_align_errors +
  2070. hwstat->rx_resource_errors +
  2071. hwstat->rx_overruns +
  2072. hwstat->rx_oversize_pkts +
  2073. hwstat->rx_jabbers +
  2074. hwstat->rx_undersize_pkts +
  2075. hwstat->rx_length_mismatch);
  2076. nstat->tx_errors = (hwstat->tx_late_cols +
  2077. hwstat->tx_excessive_cols +
  2078. hwstat->tx_underruns +
  2079. hwstat->tx_carrier_errors +
  2080. hwstat->sqe_test_errors);
  2081. nstat->collisions = (hwstat->tx_single_cols +
  2082. hwstat->tx_multiple_cols +
  2083. hwstat->tx_excessive_cols);
  2084. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  2085. hwstat->rx_jabbers +
  2086. hwstat->rx_undersize_pkts +
  2087. hwstat->rx_length_mismatch);
  2088. nstat->rx_over_errors = hwstat->rx_resource_errors +
  2089. hwstat->rx_overruns;
  2090. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  2091. nstat->rx_frame_errors = hwstat->rx_align_errors;
  2092. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2093. /* XXX: What does "missed" mean? */
  2094. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  2095. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  2096. nstat->tx_fifo_errors = hwstat->tx_underruns;
  2097. /* Don't know about heartbeat or window errors... */
  2098. return nstat;
  2099. }
  2100. static int macb_get_regs_len(struct net_device *netdev)
  2101. {
  2102. return MACB_GREGS_NBR * sizeof(u32);
  2103. }
  2104. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2105. void *p)
  2106. {
  2107. struct macb *bp = netdev_priv(dev);
  2108. unsigned int tail, head;
  2109. u32 *regs_buff = p;
  2110. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  2111. | MACB_GREGS_VERSION;
  2112. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  2113. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  2114. regs_buff[0] = macb_readl(bp, NCR);
  2115. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  2116. regs_buff[2] = macb_readl(bp, NSR);
  2117. regs_buff[3] = macb_readl(bp, TSR);
  2118. regs_buff[4] = macb_readl(bp, RBQP);
  2119. regs_buff[5] = macb_readl(bp, TBQP);
  2120. regs_buff[6] = macb_readl(bp, RSR);
  2121. regs_buff[7] = macb_readl(bp, IMR);
  2122. regs_buff[8] = tail;
  2123. regs_buff[9] = head;
  2124. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  2125. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  2126. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  2127. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  2128. if (macb_is_gem(bp))
  2129. regs_buff[13] = gem_readl(bp, DMACFG);
  2130. }
  2131. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2132. {
  2133. struct macb *bp = netdev_priv(netdev);
  2134. wol->supported = 0;
  2135. wol->wolopts = 0;
  2136. if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
  2137. wol->supported = WAKE_MAGIC;
  2138. if (bp->wol & MACB_WOL_ENABLED)
  2139. wol->wolopts |= WAKE_MAGIC;
  2140. }
  2141. }
  2142. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2143. {
  2144. struct macb *bp = netdev_priv(netdev);
  2145. if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
  2146. (wol->wolopts & ~WAKE_MAGIC))
  2147. return -EOPNOTSUPP;
  2148. if (wol->wolopts & WAKE_MAGIC)
  2149. bp->wol |= MACB_WOL_ENABLED;
  2150. else
  2151. bp->wol &= ~MACB_WOL_ENABLED;
  2152. device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
  2153. return 0;
  2154. }
  2155. static void macb_get_ringparam(struct net_device *netdev,
  2156. struct ethtool_ringparam *ring)
  2157. {
  2158. struct macb *bp = netdev_priv(netdev);
  2159. ring->rx_max_pending = MAX_RX_RING_SIZE;
  2160. ring->tx_max_pending = MAX_TX_RING_SIZE;
  2161. ring->rx_pending = bp->rx_ring_size;
  2162. ring->tx_pending = bp->tx_ring_size;
  2163. }
  2164. static int macb_set_ringparam(struct net_device *netdev,
  2165. struct ethtool_ringparam *ring)
  2166. {
  2167. struct macb *bp = netdev_priv(netdev);
  2168. u32 new_rx_size, new_tx_size;
  2169. unsigned int reset = 0;
  2170. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2171. return -EINVAL;
  2172. new_rx_size = clamp_t(u32, ring->rx_pending,
  2173. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  2174. new_rx_size = roundup_pow_of_two(new_rx_size);
  2175. new_tx_size = clamp_t(u32, ring->tx_pending,
  2176. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  2177. new_tx_size = roundup_pow_of_two(new_tx_size);
  2178. if ((new_tx_size == bp->tx_ring_size) &&
  2179. (new_rx_size == bp->rx_ring_size)) {
  2180. /* nothing to do */
  2181. return 0;
  2182. }
  2183. if (netif_running(bp->dev)) {
  2184. reset = 1;
  2185. macb_close(bp->dev);
  2186. }
  2187. bp->rx_ring_size = new_rx_size;
  2188. bp->tx_ring_size = new_tx_size;
  2189. if (reset)
  2190. macb_open(bp->dev);
  2191. return 0;
  2192. }
  2193. #ifdef CONFIG_MACB_USE_HWSTAMP
  2194. static unsigned int gem_get_tsu_rate(struct macb *bp)
  2195. {
  2196. struct clk *tsu_clk;
  2197. unsigned int tsu_rate;
  2198. tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
  2199. if (!IS_ERR(tsu_clk))
  2200. tsu_rate = clk_get_rate(tsu_clk);
  2201. /* try pclk instead */
  2202. else if (!IS_ERR(bp->pclk)) {
  2203. tsu_clk = bp->pclk;
  2204. tsu_rate = clk_get_rate(tsu_clk);
  2205. } else
  2206. return -ENOTSUPP;
  2207. return tsu_rate;
  2208. }
  2209. static s32 gem_get_ptp_max_adj(void)
  2210. {
  2211. return 64000000;
  2212. }
  2213. static int gem_get_ts_info(struct net_device *dev,
  2214. struct ethtool_ts_info *info)
  2215. {
  2216. struct macb *bp = netdev_priv(dev);
  2217. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
  2218. ethtool_op_get_ts_info(dev, info);
  2219. return 0;
  2220. }
  2221. info->so_timestamping =
  2222. SOF_TIMESTAMPING_TX_SOFTWARE |
  2223. SOF_TIMESTAMPING_RX_SOFTWARE |
  2224. SOF_TIMESTAMPING_SOFTWARE |
  2225. SOF_TIMESTAMPING_TX_HARDWARE |
  2226. SOF_TIMESTAMPING_RX_HARDWARE |
  2227. SOF_TIMESTAMPING_RAW_HARDWARE;
  2228. info->tx_types =
  2229. (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
  2230. (1 << HWTSTAMP_TX_OFF) |
  2231. (1 << HWTSTAMP_TX_ON);
  2232. info->rx_filters =
  2233. (1 << HWTSTAMP_FILTER_NONE) |
  2234. (1 << HWTSTAMP_FILTER_ALL);
  2235. info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
  2236. return 0;
  2237. }
  2238. static struct macb_ptp_info gem_ptp_info = {
  2239. .ptp_init = gem_ptp_init,
  2240. .ptp_remove = gem_ptp_remove,
  2241. .get_ptp_max_adj = gem_get_ptp_max_adj,
  2242. .get_tsu_rate = gem_get_tsu_rate,
  2243. .get_ts_info = gem_get_ts_info,
  2244. .get_hwtst = gem_get_hwtst,
  2245. .set_hwtst = gem_set_hwtst,
  2246. };
  2247. #endif
  2248. static int macb_get_ts_info(struct net_device *netdev,
  2249. struct ethtool_ts_info *info)
  2250. {
  2251. struct macb *bp = netdev_priv(netdev);
  2252. if (bp->ptp_info)
  2253. return bp->ptp_info->get_ts_info(netdev, info);
  2254. return ethtool_op_get_ts_info(netdev, info);
  2255. }
  2256. static void gem_enable_flow_filters(struct macb *bp, bool enable)
  2257. {
  2258. struct ethtool_rx_fs_item *item;
  2259. u32 t2_scr;
  2260. int num_t2_scr;
  2261. num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
  2262. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2263. struct ethtool_rx_flow_spec *fs = &item->fs;
  2264. struct ethtool_tcpip4_spec *tp4sp_m;
  2265. if (fs->location >= num_t2_scr)
  2266. continue;
  2267. t2_scr = gem_readl_n(bp, SCRT2, fs->location);
  2268. /* enable/disable screener regs for the flow entry */
  2269. t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
  2270. /* only enable fields with no masking */
  2271. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2272. if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
  2273. t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
  2274. else
  2275. t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
  2276. if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
  2277. t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
  2278. else
  2279. t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
  2280. if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
  2281. t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
  2282. else
  2283. t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
  2284. gem_writel_n(bp, SCRT2, fs->location, t2_scr);
  2285. }
  2286. }
  2287. static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
  2288. {
  2289. struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
  2290. uint16_t index = fs->location;
  2291. u32 w0, w1, t2_scr;
  2292. bool cmp_a = false;
  2293. bool cmp_b = false;
  2294. bool cmp_c = false;
  2295. tp4sp_v = &(fs->h_u.tcp_ip4_spec);
  2296. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  2297. /* ignore field if any masking set */
  2298. if (tp4sp_m->ip4src == 0xFFFFFFFF) {
  2299. /* 1st compare reg - IP source address */
  2300. w0 = 0;
  2301. w1 = 0;
  2302. w0 = tp4sp_v->ip4src;
  2303. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2304. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2305. w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
  2306. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
  2307. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
  2308. cmp_a = true;
  2309. }
  2310. /* ignore field if any masking set */
  2311. if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
  2312. /* 2nd compare reg - IP destination address */
  2313. w0 = 0;
  2314. w1 = 0;
  2315. w0 = tp4sp_v->ip4dst;
  2316. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2317. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  2318. w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
  2319. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
  2320. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
  2321. cmp_b = true;
  2322. }
  2323. /* ignore both port fields if masking set in both */
  2324. if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
  2325. /* 3rd compare reg - source port, destination port */
  2326. w0 = 0;
  2327. w1 = 0;
  2328. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
  2329. if (tp4sp_m->psrc == tp4sp_m->pdst) {
  2330. w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
  2331. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2332. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  2333. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2334. } else {
  2335. /* only one port definition */
  2336. w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
  2337. w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
  2338. if (tp4sp_m->psrc == 0xFFFF) { /* src port */
  2339. w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
  2340. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  2341. } else { /* dst port */
  2342. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  2343. w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
  2344. }
  2345. }
  2346. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
  2347. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
  2348. cmp_c = true;
  2349. }
  2350. t2_scr = 0;
  2351. t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
  2352. t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
  2353. if (cmp_a)
  2354. t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
  2355. if (cmp_b)
  2356. t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
  2357. if (cmp_c)
  2358. t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
  2359. gem_writel_n(bp, SCRT2, index, t2_scr);
  2360. }
  2361. static int gem_add_flow_filter(struct net_device *netdev,
  2362. struct ethtool_rxnfc *cmd)
  2363. {
  2364. struct macb *bp = netdev_priv(netdev);
  2365. struct ethtool_rx_flow_spec *fs = &cmd->fs;
  2366. struct ethtool_rx_fs_item *item, *newfs;
  2367. unsigned long flags;
  2368. int ret = -EINVAL;
  2369. bool added = false;
  2370. newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
  2371. if (newfs == NULL)
  2372. return -ENOMEM;
  2373. memcpy(&newfs->fs, fs, sizeof(newfs->fs));
  2374. netdev_dbg(netdev,
  2375. "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2376. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2377. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2378. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2379. htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
  2380. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2381. /* find correct place to add in list */
  2382. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2383. if (item->fs.location > newfs->fs.location) {
  2384. list_add_tail(&newfs->list, &item->list);
  2385. added = true;
  2386. break;
  2387. } else if (item->fs.location == fs->location) {
  2388. netdev_err(netdev, "Rule not added: location %d not free!\n",
  2389. fs->location);
  2390. ret = -EBUSY;
  2391. goto err;
  2392. }
  2393. }
  2394. if (!added)
  2395. list_add_tail(&newfs->list, &bp->rx_fs_list.list);
  2396. gem_prog_cmp_regs(bp, fs);
  2397. bp->rx_fs_list.count++;
  2398. /* enable filtering if NTUPLE on */
  2399. if (netdev->features & NETIF_F_NTUPLE)
  2400. gem_enable_flow_filters(bp, 1);
  2401. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2402. return 0;
  2403. err:
  2404. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2405. kfree(newfs);
  2406. return ret;
  2407. }
  2408. static int gem_del_flow_filter(struct net_device *netdev,
  2409. struct ethtool_rxnfc *cmd)
  2410. {
  2411. struct macb *bp = netdev_priv(netdev);
  2412. struct ethtool_rx_fs_item *item;
  2413. struct ethtool_rx_flow_spec *fs;
  2414. unsigned long flags;
  2415. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  2416. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2417. if (item->fs.location == cmd->fs.location) {
  2418. /* disable screener regs for the flow entry */
  2419. fs = &(item->fs);
  2420. netdev_dbg(netdev,
  2421. "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  2422. fs->flow_type, (int)fs->ring_cookie, fs->location,
  2423. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  2424. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  2425. htons(fs->h_u.tcp_ip4_spec.psrc),
  2426. htons(fs->h_u.tcp_ip4_spec.pdst));
  2427. gem_writel_n(bp, SCRT2, fs->location, 0);
  2428. list_del(&item->list);
  2429. bp->rx_fs_list.count--;
  2430. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2431. kfree(item);
  2432. return 0;
  2433. }
  2434. }
  2435. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  2436. return -EINVAL;
  2437. }
  2438. static int gem_get_flow_entry(struct net_device *netdev,
  2439. struct ethtool_rxnfc *cmd)
  2440. {
  2441. struct macb *bp = netdev_priv(netdev);
  2442. struct ethtool_rx_fs_item *item;
  2443. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2444. if (item->fs.location == cmd->fs.location) {
  2445. memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
  2446. return 0;
  2447. }
  2448. }
  2449. return -EINVAL;
  2450. }
  2451. static int gem_get_all_flow_entries(struct net_device *netdev,
  2452. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  2453. {
  2454. struct macb *bp = netdev_priv(netdev);
  2455. struct ethtool_rx_fs_item *item;
  2456. uint32_t cnt = 0;
  2457. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  2458. if (cnt == cmd->rule_cnt)
  2459. return -EMSGSIZE;
  2460. rule_locs[cnt] = item->fs.location;
  2461. cnt++;
  2462. }
  2463. cmd->data = bp->max_tuples;
  2464. cmd->rule_cnt = cnt;
  2465. return 0;
  2466. }
  2467. static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  2468. u32 *rule_locs)
  2469. {
  2470. struct macb *bp = netdev_priv(netdev);
  2471. int ret = 0;
  2472. switch (cmd->cmd) {
  2473. case ETHTOOL_GRXRINGS:
  2474. cmd->data = bp->num_queues;
  2475. break;
  2476. case ETHTOOL_GRXCLSRLCNT:
  2477. cmd->rule_cnt = bp->rx_fs_list.count;
  2478. break;
  2479. case ETHTOOL_GRXCLSRULE:
  2480. ret = gem_get_flow_entry(netdev, cmd);
  2481. break;
  2482. case ETHTOOL_GRXCLSRLALL:
  2483. ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
  2484. break;
  2485. default:
  2486. netdev_err(netdev,
  2487. "Command parameter %d is not supported\n", cmd->cmd);
  2488. ret = -EOPNOTSUPP;
  2489. }
  2490. return ret;
  2491. }
  2492. static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  2493. {
  2494. struct macb *bp = netdev_priv(netdev);
  2495. int ret;
  2496. switch (cmd->cmd) {
  2497. case ETHTOOL_SRXCLSRLINS:
  2498. if ((cmd->fs.location >= bp->max_tuples)
  2499. || (cmd->fs.ring_cookie >= bp->num_queues)) {
  2500. ret = -EINVAL;
  2501. break;
  2502. }
  2503. ret = gem_add_flow_filter(netdev, cmd);
  2504. break;
  2505. case ETHTOOL_SRXCLSRLDEL:
  2506. ret = gem_del_flow_filter(netdev, cmd);
  2507. break;
  2508. default:
  2509. netdev_err(netdev,
  2510. "Command parameter %d is not supported\n", cmd->cmd);
  2511. ret = -EOPNOTSUPP;
  2512. }
  2513. return ret;
  2514. }
  2515. static const struct ethtool_ops macb_ethtool_ops = {
  2516. .get_regs_len = macb_get_regs_len,
  2517. .get_regs = macb_get_regs,
  2518. .get_link = ethtool_op_get_link,
  2519. .get_ts_info = ethtool_op_get_ts_info,
  2520. .get_wol = macb_get_wol,
  2521. .set_wol = macb_set_wol,
  2522. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2523. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2524. .get_ringparam = macb_get_ringparam,
  2525. .set_ringparam = macb_set_ringparam,
  2526. };
  2527. static const struct ethtool_ops gem_ethtool_ops = {
  2528. .get_regs_len = macb_get_regs_len,
  2529. .get_regs = macb_get_regs,
  2530. .get_link = ethtool_op_get_link,
  2531. .get_ts_info = macb_get_ts_info,
  2532. .get_ethtool_stats = gem_get_ethtool_stats,
  2533. .get_strings = gem_get_ethtool_strings,
  2534. .get_sset_count = gem_get_sset_count,
  2535. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2536. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2537. .get_ringparam = macb_get_ringparam,
  2538. .set_ringparam = macb_set_ringparam,
  2539. .get_rxnfc = gem_get_rxnfc,
  2540. .set_rxnfc = gem_set_rxnfc,
  2541. };
  2542. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2543. {
  2544. struct phy_device *phydev = dev->phydev;
  2545. struct macb *bp = netdev_priv(dev);
  2546. if (!netif_running(dev))
  2547. return -EINVAL;
  2548. if (!phydev)
  2549. return -ENODEV;
  2550. if (!bp->ptp_info)
  2551. return phy_mii_ioctl(phydev, rq, cmd);
  2552. switch (cmd) {
  2553. case SIOCSHWTSTAMP:
  2554. return bp->ptp_info->set_hwtst(dev, rq, cmd);
  2555. case SIOCGHWTSTAMP:
  2556. return bp->ptp_info->get_hwtst(dev, rq);
  2557. default:
  2558. return phy_mii_ioctl(phydev, rq, cmd);
  2559. }
  2560. }
  2561. static int macb_set_features(struct net_device *netdev,
  2562. netdev_features_t features)
  2563. {
  2564. struct macb *bp = netdev_priv(netdev);
  2565. netdev_features_t changed = features ^ netdev->features;
  2566. /* TX checksum offload */
  2567. if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
  2568. u32 dmacfg;
  2569. dmacfg = gem_readl(bp, DMACFG);
  2570. if (features & NETIF_F_HW_CSUM)
  2571. dmacfg |= GEM_BIT(TXCOEN);
  2572. else
  2573. dmacfg &= ~GEM_BIT(TXCOEN);
  2574. gem_writel(bp, DMACFG, dmacfg);
  2575. }
  2576. /* RX checksum offload */
  2577. if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
  2578. u32 netcfg;
  2579. netcfg = gem_readl(bp, NCFGR);
  2580. if (features & NETIF_F_RXCSUM &&
  2581. !(netdev->flags & IFF_PROMISC))
  2582. netcfg |= GEM_BIT(RXCOEN);
  2583. else
  2584. netcfg &= ~GEM_BIT(RXCOEN);
  2585. gem_writel(bp, NCFGR, netcfg);
  2586. }
  2587. /* RX Flow Filters */
  2588. if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
  2589. bool turn_on = features & NETIF_F_NTUPLE;
  2590. gem_enable_flow_filters(bp, turn_on);
  2591. }
  2592. return 0;
  2593. }
  2594. static const struct net_device_ops macb_netdev_ops = {
  2595. .ndo_open = macb_open,
  2596. .ndo_stop = macb_close,
  2597. .ndo_start_xmit = macb_start_xmit,
  2598. .ndo_set_rx_mode = macb_set_rx_mode,
  2599. .ndo_get_stats = macb_get_stats,
  2600. .ndo_do_ioctl = macb_ioctl,
  2601. .ndo_validate_addr = eth_validate_addr,
  2602. .ndo_change_mtu = macb_change_mtu,
  2603. .ndo_set_mac_address = eth_mac_addr,
  2604. #ifdef CONFIG_NET_POLL_CONTROLLER
  2605. .ndo_poll_controller = macb_poll_controller,
  2606. #endif
  2607. .ndo_set_features = macb_set_features,
  2608. .ndo_features_check = macb_features_check,
  2609. };
  2610. /* Configure peripheral capabilities according to device tree
  2611. * and integration options used
  2612. */
  2613. static void macb_configure_caps(struct macb *bp,
  2614. const struct macb_config *dt_conf)
  2615. {
  2616. u32 dcfg;
  2617. if (dt_conf)
  2618. bp->caps = dt_conf->caps;
  2619. if (hw_is_gem(bp->regs, bp->native_io)) {
  2620. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  2621. dcfg = gem_readl(bp, DCFG1);
  2622. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  2623. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  2624. dcfg = gem_readl(bp, DCFG2);
  2625. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  2626. bp->caps |= MACB_CAPS_FIFO_MODE;
  2627. #ifdef CONFIG_MACB_USE_HWSTAMP
  2628. if (gem_has_ptp(bp)) {
  2629. if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
  2630. pr_err("GEM doesn't support hardware ptp.\n");
  2631. else {
  2632. bp->hw_dma_cap |= HW_DMA_CAP_PTP;
  2633. bp->ptp_info = &gem_ptp_info;
  2634. }
  2635. }
  2636. #endif
  2637. }
  2638. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  2639. }
  2640. static void macb_probe_queues(void __iomem *mem,
  2641. bool native_io,
  2642. unsigned int *queue_mask,
  2643. unsigned int *num_queues)
  2644. {
  2645. unsigned int hw_q;
  2646. *queue_mask = 0x1;
  2647. *num_queues = 1;
  2648. /* is it macb or gem ?
  2649. *
  2650. * We need to read directly from the hardware here because
  2651. * we are early in the probe process and don't have the
  2652. * MACB_CAPS_MACB_IS_GEM flag positioned
  2653. */
  2654. if (!hw_is_gem(mem, native_io))
  2655. return;
  2656. /* bit 0 is never set but queue 0 always exists */
  2657. *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
  2658. *queue_mask |= 0x1;
  2659. for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
  2660. if (*queue_mask & (1 << hw_q))
  2661. (*num_queues)++;
  2662. }
  2663. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  2664. struct clk **hclk, struct clk **tx_clk,
  2665. struct clk **rx_clk)
  2666. {
  2667. struct macb_platform_data *pdata;
  2668. int err;
  2669. pdata = dev_get_platdata(&pdev->dev);
  2670. if (pdata) {
  2671. *pclk = pdata->pclk;
  2672. *hclk = pdata->hclk;
  2673. } else {
  2674. *pclk = devm_clk_get(&pdev->dev, "pclk");
  2675. *hclk = devm_clk_get(&pdev->dev, "hclk");
  2676. }
  2677. if (IS_ERR(*pclk)) {
  2678. err = PTR_ERR(*pclk);
  2679. dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
  2680. return err;
  2681. }
  2682. if (IS_ERR(*hclk)) {
  2683. err = PTR_ERR(*hclk);
  2684. dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
  2685. return err;
  2686. }
  2687. *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
  2688. if (IS_ERR(*tx_clk))
  2689. *tx_clk = NULL;
  2690. *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
  2691. if (IS_ERR(*rx_clk))
  2692. *rx_clk = NULL;
  2693. err = clk_prepare_enable(*pclk);
  2694. if (err) {
  2695. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  2696. return err;
  2697. }
  2698. err = clk_prepare_enable(*hclk);
  2699. if (err) {
  2700. dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
  2701. goto err_disable_pclk;
  2702. }
  2703. err = clk_prepare_enable(*tx_clk);
  2704. if (err) {
  2705. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  2706. goto err_disable_hclk;
  2707. }
  2708. err = clk_prepare_enable(*rx_clk);
  2709. if (err) {
  2710. dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
  2711. goto err_disable_txclk;
  2712. }
  2713. return 0;
  2714. err_disable_txclk:
  2715. clk_disable_unprepare(*tx_clk);
  2716. err_disable_hclk:
  2717. clk_disable_unprepare(*hclk);
  2718. err_disable_pclk:
  2719. clk_disable_unprepare(*pclk);
  2720. return err;
  2721. }
  2722. static int macb_init(struct platform_device *pdev)
  2723. {
  2724. struct net_device *dev = platform_get_drvdata(pdev);
  2725. unsigned int hw_q, q;
  2726. struct macb *bp = netdev_priv(dev);
  2727. struct macb_queue *queue;
  2728. int err;
  2729. u32 val, reg;
  2730. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  2731. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  2732. /* set the queue register mapping once for all: queue0 has a special
  2733. * register mapping but we don't want to test the queue index then
  2734. * compute the corresponding register offset at run time.
  2735. */
  2736. for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
  2737. if (!(bp->queue_mask & (1 << hw_q)))
  2738. continue;
  2739. queue = &bp->queues[q];
  2740. queue->bp = bp;
  2741. netif_napi_add(dev, &queue->napi, macb_poll, 64);
  2742. if (hw_q) {
  2743. queue->ISR = GEM_ISR(hw_q - 1);
  2744. queue->IER = GEM_IER(hw_q - 1);
  2745. queue->IDR = GEM_IDR(hw_q - 1);
  2746. queue->IMR = GEM_IMR(hw_q - 1);
  2747. queue->TBQP = GEM_TBQP(hw_q - 1);
  2748. queue->RBQP = GEM_RBQP(hw_q - 1);
  2749. queue->RBQS = GEM_RBQS(hw_q - 1);
  2750. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2751. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2752. queue->TBQPH = GEM_TBQPH(hw_q - 1);
  2753. queue->RBQPH = GEM_RBQPH(hw_q - 1);
  2754. }
  2755. #endif
  2756. } else {
  2757. /* queue0 uses legacy registers */
  2758. queue->ISR = MACB_ISR;
  2759. queue->IER = MACB_IER;
  2760. queue->IDR = MACB_IDR;
  2761. queue->IMR = MACB_IMR;
  2762. queue->TBQP = MACB_TBQP;
  2763. queue->RBQP = MACB_RBQP;
  2764. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  2765. if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
  2766. queue->TBQPH = MACB_TBQPH;
  2767. queue->RBQPH = MACB_RBQPH;
  2768. }
  2769. #endif
  2770. }
  2771. /* get irq: here we use the linux queue index, not the hardware
  2772. * queue index. the queue irq definitions in the device tree
  2773. * must remove the optional gaps that could exist in the
  2774. * hardware queue mask.
  2775. */
  2776. queue->irq = platform_get_irq(pdev, q);
  2777. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  2778. IRQF_SHARED, dev->name, queue);
  2779. if (err) {
  2780. dev_err(&pdev->dev,
  2781. "Unable to request IRQ %d (error %d)\n",
  2782. queue->irq, err);
  2783. return err;
  2784. }
  2785. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  2786. q++;
  2787. }
  2788. dev->netdev_ops = &macb_netdev_ops;
  2789. /* setup appropriated routines according to adapter type */
  2790. if (macb_is_gem(bp)) {
  2791. bp->max_tx_length = GEM_MAX_TX_LEN;
  2792. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  2793. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  2794. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  2795. bp->macbgem_ops.mog_rx = gem_rx;
  2796. dev->ethtool_ops = &gem_ethtool_ops;
  2797. } else {
  2798. bp->max_tx_length = MACB_MAX_TX_LEN;
  2799. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  2800. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  2801. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  2802. bp->macbgem_ops.mog_rx = macb_rx;
  2803. dev->ethtool_ops = &macb_ethtool_ops;
  2804. }
  2805. /* Set features */
  2806. dev->hw_features = NETIF_F_SG;
  2807. /* Check LSO capability */
  2808. if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  2809. dev->hw_features |= MACB_NETIF_LSO;
  2810. /* Checksum offload is only available on gem with packet buffer */
  2811. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  2812. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  2813. if (bp->caps & MACB_CAPS_SG_DISABLED)
  2814. dev->hw_features &= ~NETIF_F_SG;
  2815. dev->features = dev->hw_features;
  2816. /* Check RX Flow Filters support.
  2817. * Max Rx flows set by availability of screeners & compare regs:
  2818. * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
  2819. */
  2820. reg = gem_readl(bp, DCFG8);
  2821. bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
  2822. GEM_BFEXT(T2SCR, reg));
  2823. if (bp->max_tuples > 0) {
  2824. /* also needs one ethtype match to check IPv4 */
  2825. if (GEM_BFEXT(SCR2ETH, reg) > 0) {
  2826. /* program this reg now */
  2827. reg = 0;
  2828. reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
  2829. gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
  2830. /* Filtering is supported in hw but don't enable it in kernel now */
  2831. dev->hw_features |= NETIF_F_NTUPLE;
  2832. /* init Rx flow definitions */
  2833. INIT_LIST_HEAD(&bp->rx_fs_list.list);
  2834. bp->rx_fs_list.count = 0;
  2835. spin_lock_init(&bp->rx_fs_lock);
  2836. } else
  2837. bp->max_tuples = 0;
  2838. }
  2839. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  2840. val = 0;
  2841. if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
  2842. val = GEM_BIT(RGMII);
  2843. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  2844. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2845. val = MACB_BIT(RMII);
  2846. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  2847. val = MACB_BIT(MII);
  2848. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  2849. val |= MACB_BIT(CLKEN);
  2850. macb_or_gem_writel(bp, USRIO, val);
  2851. }
  2852. /* Set MII management clock divider */
  2853. val = macb_mdc_clk_div(bp);
  2854. val |= macb_dbw(bp);
  2855. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2856. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  2857. macb_writel(bp, NCFGR, val);
  2858. return 0;
  2859. }
  2860. #if defined(CONFIG_OF)
  2861. /* 1518 rounded up */
  2862. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  2863. /* max number of receive buffers */
  2864. #define AT91ETHER_MAX_RX_DESCR 9
  2865. /* Initialize and start the Receiver and Transmit subsystems */
  2866. static int at91ether_start(struct net_device *dev)
  2867. {
  2868. struct macb *lp = netdev_priv(dev);
  2869. struct macb_queue *q = &lp->queues[0];
  2870. struct macb_dma_desc *desc;
  2871. dma_addr_t addr;
  2872. u32 ctl;
  2873. int i;
  2874. q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  2875. (AT91ETHER_MAX_RX_DESCR *
  2876. macb_dma_desc_get_size(lp)),
  2877. &q->rx_ring_dma, GFP_KERNEL);
  2878. if (!q->rx_ring)
  2879. return -ENOMEM;
  2880. q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  2881. AT91ETHER_MAX_RX_DESCR *
  2882. AT91ETHER_MAX_RBUFF_SZ,
  2883. &q->rx_buffers_dma, GFP_KERNEL);
  2884. if (!q->rx_buffers) {
  2885. dma_free_coherent(&lp->pdev->dev,
  2886. AT91ETHER_MAX_RX_DESCR *
  2887. macb_dma_desc_get_size(lp),
  2888. q->rx_ring, q->rx_ring_dma);
  2889. q->rx_ring = NULL;
  2890. return -ENOMEM;
  2891. }
  2892. addr = q->rx_buffers_dma;
  2893. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  2894. desc = macb_rx_desc(q, i);
  2895. macb_set_addr(lp, desc, addr);
  2896. desc->ctrl = 0;
  2897. addr += AT91ETHER_MAX_RBUFF_SZ;
  2898. }
  2899. /* Set the Wrap bit on the last descriptor */
  2900. desc->addr |= MACB_BIT(RX_WRAP);
  2901. /* Reset buffer index */
  2902. q->rx_tail = 0;
  2903. /* Program address of descriptor list in Rx Buffer Queue register */
  2904. macb_writel(lp, RBQP, q->rx_ring_dma);
  2905. /* Enable Receive and Transmit */
  2906. ctl = macb_readl(lp, NCR);
  2907. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  2908. return 0;
  2909. }
  2910. /* Open the ethernet interface */
  2911. static int at91ether_open(struct net_device *dev)
  2912. {
  2913. struct macb *lp = netdev_priv(dev);
  2914. u32 ctl;
  2915. int ret;
  2916. /* Clear internal statistics */
  2917. ctl = macb_readl(lp, NCR);
  2918. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  2919. macb_set_hwaddr(lp);
  2920. ret = at91ether_start(dev);
  2921. if (ret)
  2922. return ret;
  2923. /* Enable MAC interrupts */
  2924. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  2925. MACB_BIT(RXUBR) |
  2926. MACB_BIT(ISR_TUND) |
  2927. MACB_BIT(ISR_RLE) |
  2928. MACB_BIT(TCOMP) |
  2929. MACB_BIT(ISR_ROVR) |
  2930. MACB_BIT(HRESP));
  2931. /* schedule a link state check */
  2932. phy_start(dev->phydev);
  2933. netif_start_queue(dev);
  2934. return 0;
  2935. }
  2936. /* Close the interface */
  2937. static int at91ether_close(struct net_device *dev)
  2938. {
  2939. struct macb *lp = netdev_priv(dev);
  2940. struct macb_queue *q = &lp->queues[0];
  2941. u32 ctl;
  2942. /* Disable Receiver and Transmitter */
  2943. ctl = macb_readl(lp, NCR);
  2944. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  2945. /* Disable MAC interrupts */
  2946. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  2947. MACB_BIT(RXUBR) |
  2948. MACB_BIT(ISR_TUND) |
  2949. MACB_BIT(ISR_RLE) |
  2950. MACB_BIT(TCOMP) |
  2951. MACB_BIT(ISR_ROVR) |
  2952. MACB_BIT(HRESP));
  2953. netif_stop_queue(dev);
  2954. dma_free_coherent(&lp->pdev->dev,
  2955. AT91ETHER_MAX_RX_DESCR *
  2956. macb_dma_desc_get_size(lp),
  2957. q->rx_ring, q->rx_ring_dma);
  2958. q->rx_ring = NULL;
  2959. dma_free_coherent(&lp->pdev->dev,
  2960. AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
  2961. q->rx_buffers, q->rx_buffers_dma);
  2962. q->rx_buffers = NULL;
  2963. return 0;
  2964. }
  2965. /* Transmit packet */
  2966. static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2967. {
  2968. struct macb *lp = netdev_priv(dev);
  2969. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  2970. netif_stop_queue(dev);
  2971. /* Store packet information (to free when Tx completed) */
  2972. lp->skb = skb;
  2973. lp->skb_length = skb->len;
  2974. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
  2975. DMA_TO_DEVICE);
  2976. if (dma_mapping_error(NULL, lp->skb_physaddr)) {
  2977. dev_kfree_skb_any(skb);
  2978. dev->stats.tx_dropped++;
  2979. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  2980. return NETDEV_TX_OK;
  2981. }
  2982. /* Set address of the data in the Transmit Address register */
  2983. macb_writel(lp, TAR, lp->skb_physaddr);
  2984. /* Set length of the packet in the Transmit Control register */
  2985. macb_writel(lp, TCR, skb->len);
  2986. } else {
  2987. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  2988. return NETDEV_TX_BUSY;
  2989. }
  2990. return NETDEV_TX_OK;
  2991. }
  2992. /* Extract received frame from buffer descriptors and sent to upper layers.
  2993. * (Called from interrupt context)
  2994. */
  2995. static void at91ether_rx(struct net_device *dev)
  2996. {
  2997. struct macb *lp = netdev_priv(dev);
  2998. struct macb_queue *q = &lp->queues[0];
  2999. struct macb_dma_desc *desc;
  3000. unsigned char *p_recv;
  3001. struct sk_buff *skb;
  3002. unsigned int pktlen;
  3003. desc = macb_rx_desc(q, q->rx_tail);
  3004. while (desc->addr & MACB_BIT(RX_USED)) {
  3005. p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  3006. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  3007. skb = netdev_alloc_skb(dev, pktlen + 2);
  3008. if (skb) {
  3009. skb_reserve(skb, 2);
  3010. skb_put_data(skb, p_recv, pktlen);
  3011. skb->protocol = eth_type_trans(skb, dev);
  3012. dev->stats.rx_packets++;
  3013. dev->stats.rx_bytes += pktlen;
  3014. netif_rx(skb);
  3015. } else {
  3016. dev->stats.rx_dropped++;
  3017. }
  3018. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  3019. dev->stats.multicast++;
  3020. /* reset ownership bit */
  3021. desc->addr &= ~MACB_BIT(RX_USED);
  3022. /* wrap after last buffer */
  3023. if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  3024. q->rx_tail = 0;
  3025. else
  3026. q->rx_tail++;
  3027. desc = macb_rx_desc(q, q->rx_tail);
  3028. }
  3029. }
  3030. /* MAC interrupt handler */
  3031. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  3032. {
  3033. struct net_device *dev = dev_id;
  3034. struct macb *lp = netdev_priv(dev);
  3035. u32 intstatus, ctl;
  3036. /* MAC Interrupt Status register indicates what interrupts are pending.
  3037. * It is automatically cleared once read.
  3038. */
  3039. intstatus = macb_readl(lp, ISR);
  3040. /* Receive complete */
  3041. if (intstatus & MACB_BIT(RCOMP))
  3042. at91ether_rx(dev);
  3043. /* Transmit complete */
  3044. if (intstatus & MACB_BIT(TCOMP)) {
  3045. /* The TCOM bit is set even if the transmission failed */
  3046. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  3047. dev->stats.tx_errors++;
  3048. if (lp->skb) {
  3049. dev_kfree_skb_irq(lp->skb);
  3050. lp->skb = NULL;
  3051. dma_unmap_single(NULL, lp->skb_physaddr,
  3052. lp->skb_length, DMA_TO_DEVICE);
  3053. dev->stats.tx_packets++;
  3054. dev->stats.tx_bytes += lp->skb_length;
  3055. }
  3056. netif_wake_queue(dev);
  3057. }
  3058. /* Work-around for EMAC Errata section 41.3.1 */
  3059. if (intstatus & MACB_BIT(RXUBR)) {
  3060. ctl = macb_readl(lp, NCR);
  3061. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  3062. wmb();
  3063. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  3064. }
  3065. if (intstatus & MACB_BIT(ISR_ROVR))
  3066. netdev_err(dev, "ROVR error\n");
  3067. return IRQ_HANDLED;
  3068. }
  3069. #ifdef CONFIG_NET_POLL_CONTROLLER
  3070. static void at91ether_poll_controller(struct net_device *dev)
  3071. {
  3072. unsigned long flags;
  3073. local_irq_save(flags);
  3074. at91ether_interrupt(dev->irq, dev);
  3075. local_irq_restore(flags);
  3076. }
  3077. #endif
  3078. static const struct net_device_ops at91ether_netdev_ops = {
  3079. .ndo_open = at91ether_open,
  3080. .ndo_stop = at91ether_close,
  3081. .ndo_start_xmit = at91ether_start_xmit,
  3082. .ndo_get_stats = macb_get_stats,
  3083. .ndo_set_rx_mode = macb_set_rx_mode,
  3084. .ndo_set_mac_address = eth_mac_addr,
  3085. .ndo_do_ioctl = macb_ioctl,
  3086. .ndo_validate_addr = eth_validate_addr,
  3087. #ifdef CONFIG_NET_POLL_CONTROLLER
  3088. .ndo_poll_controller = at91ether_poll_controller,
  3089. #endif
  3090. };
  3091. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  3092. struct clk **hclk, struct clk **tx_clk,
  3093. struct clk **rx_clk)
  3094. {
  3095. int err;
  3096. *hclk = NULL;
  3097. *tx_clk = NULL;
  3098. *rx_clk = NULL;
  3099. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  3100. if (IS_ERR(*pclk))
  3101. return PTR_ERR(*pclk);
  3102. err = clk_prepare_enable(*pclk);
  3103. if (err) {
  3104. dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
  3105. return err;
  3106. }
  3107. return 0;
  3108. }
  3109. static int at91ether_init(struct platform_device *pdev)
  3110. {
  3111. struct net_device *dev = platform_get_drvdata(pdev);
  3112. struct macb *bp = netdev_priv(dev);
  3113. int err;
  3114. u32 reg;
  3115. bp->queues[0].bp = bp;
  3116. dev->netdev_ops = &at91ether_netdev_ops;
  3117. dev->ethtool_ops = &macb_ethtool_ops;
  3118. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  3119. 0, dev->name, dev);
  3120. if (err)
  3121. return err;
  3122. macb_writel(bp, NCR, 0);
  3123. reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
  3124. if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
  3125. reg |= MACB_BIT(RM9200_RMII);
  3126. macb_writel(bp, NCFGR, reg);
  3127. return 0;
  3128. }
  3129. static const struct macb_config at91sam9260_config = {
  3130. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3131. .clk_init = macb_clk_init,
  3132. .init = macb_init,
  3133. };
  3134. static const struct macb_config pc302gem_config = {
  3135. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  3136. .dma_burst_length = 16,
  3137. .clk_init = macb_clk_init,
  3138. .init = macb_init,
  3139. };
  3140. static const struct macb_config sama5d2_config = {
  3141. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3142. .dma_burst_length = 16,
  3143. .clk_init = macb_clk_init,
  3144. .init = macb_init,
  3145. };
  3146. static const struct macb_config sama5d3_config = {
  3147. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
  3148. | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  3149. .dma_burst_length = 16,
  3150. .clk_init = macb_clk_init,
  3151. .init = macb_init,
  3152. .jumbo_max_len = 10240,
  3153. };
  3154. static const struct macb_config sama5d4_config = {
  3155. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  3156. .dma_burst_length = 4,
  3157. .clk_init = macb_clk_init,
  3158. .init = macb_init,
  3159. };
  3160. static const struct macb_config emac_config = {
  3161. .clk_init = at91ether_clk_init,
  3162. .init = at91ether_init,
  3163. };
  3164. static const struct macb_config np4_config = {
  3165. .caps = MACB_CAPS_USRIO_DISABLED,
  3166. .clk_init = macb_clk_init,
  3167. .init = macb_init,
  3168. };
  3169. static const struct macb_config zynqmp_config = {
  3170. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3171. MACB_CAPS_JUMBO |
  3172. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
  3173. .dma_burst_length = 16,
  3174. .clk_init = macb_clk_init,
  3175. .init = macb_init,
  3176. .jumbo_max_len = 10240,
  3177. };
  3178. static const struct macb_config zynq_config = {
  3179. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
  3180. .dma_burst_length = 16,
  3181. .clk_init = macb_clk_init,
  3182. .init = macb_init,
  3183. };
  3184. static const struct of_device_id macb_dt_ids[] = {
  3185. { .compatible = "cdns,at32ap7000-macb" },
  3186. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  3187. { .compatible = "cdns,macb" },
  3188. { .compatible = "cdns,np4-macb", .data = &np4_config },
  3189. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  3190. { .compatible = "cdns,gem", .data = &pc302gem_config },
  3191. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  3192. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  3193. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  3194. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  3195. { .compatible = "cdns,emac", .data = &emac_config },
  3196. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
  3197. { .compatible = "cdns,zynq-gem", .data = &zynq_config },
  3198. { /* sentinel */ }
  3199. };
  3200. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  3201. #endif /* CONFIG_OF */
  3202. static const struct macb_config default_gem_config = {
  3203. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  3204. MACB_CAPS_JUMBO |
  3205. MACB_CAPS_GEM_HAS_PTP,
  3206. .dma_burst_length = 16,
  3207. .clk_init = macb_clk_init,
  3208. .init = macb_init,
  3209. .jumbo_max_len = 10240,
  3210. };
  3211. static int macb_probe(struct platform_device *pdev)
  3212. {
  3213. const struct macb_config *macb_config = &default_gem_config;
  3214. int (*clk_init)(struct platform_device *, struct clk **,
  3215. struct clk **, struct clk **, struct clk **)
  3216. = macb_config->clk_init;
  3217. int (*init)(struct platform_device *) = macb_config->init;
  3218. struct device_node *np = pdev->dev.of_node;
  3219. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  3220. unsigned int queue_mask, num_queues;
  3221. struct macb_platform_data *pdata;
  3222. bool native_io;
  3223. struct phy_device *phydev;
  3224. struct net_device *dev;
  3225. struct resource *regs;
  3226. void __iomem *mem;
  3227. const char *mac;
  3228. struct macb *bp;
  3229. int err, val;
  3230. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3231. mem = devm_ioremap_resource(&pdev->dev, regs);
  3232. if (IS_ERR(mem))
  3233. return PTR_ERR(mem);
  3234. if (np) {
  3235. const struct of_device_id *match;
  3236. match = of_match_node(macb_dt_ids, np);
  3237. if (match && match->data) {
  3238. macb_config = match->data;
  3239. clk_init = macb_config->clk_init;
  3240. init = macb_config->init;
  3241. }
  3242. }
  3243. err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
  3244. if (err)
  3245. return err;
  3246. native_io = hw_is_native_io(mem);
  3247. macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
  3248. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  3249. if (!dev) {
  3250. err = -ENOMEM;
  3251. goto err_disable_clocks;
  3252. }
  3253. dev->base_addr = regs->start;
  3254. SET_NETDEV_DEV(dev, &pdev->dev);
  3255. bp = netdev_priv(dev);
  3256. bp->pdev = pdev;
  3257. bp->dev = dev;
  3258. bp->regs = mem;
  3259. bp->native_io = native_io;
  3260. if (native_io) {
  3261. bp->macb_reg_readl = hw_readl_native;
  3262. bp->macb_reg_writel = hw_writel_native;
  3263. } else {
  3264. bp->macb_reg_readl = hw_readl;
  3265. bp->macb_reg_writel = hw_writel;
  3266. }
  3267. bp->num_queues = num_queues;
  3268. bp->queue_mask = queue_mask;
  3269. if (macb_config)
  3270. bp->dma_burst_length = macb_config->dma_burst_length;
  3271. bp->pclk = pclk;
  3272. bp->hclk = hclk;
  3273. bp->tx_clk = tx_clk;
  3274. bp->rx_clk = rx_clk;
  3275. if (macb_config)
  3276. bp->jumbo_max_len = macb_config->jumbo_max_len;
  3277. bp->wol = 0;
  3278. if (of_get_property(np, "magic-packet", NULL))
  3279. bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
  3280. device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
  3281. spin_lock_init(&bp->lock);
  3282. /* setup capabilities */
  3283. macb_configure_caps(bp, macb_config);
  3284. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  3285. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  3286. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  3287. bp->hw_dma_cap |= HW_DMA_CAP_64B;
  3288. }
  3289. #endif
  3290. platform_set_drvdata(pdev, dev);
  3291. dev->irq = platform_get_irq(pdev, 0);
  3292. if (dev->irq < 0) {
  3293. err = dev->irq;
  3294. goto err_out_free_netdev;
  3295. }
  3296. /* MTU range: 68 - 1500 or 10240 */
  3297. dev->min_mtu = GEM_MTU_MIN_SIZE;
  3298. if (bp->caps & MACB_CAPS_JUMBO)
  3299. dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
  3300. else
  3301. dev->max_mtu = ETH_DATA_LEN;
  3302. if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
  3303. val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
  3304. if (val)
  3305. bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
  3306. macb_dma_desc_get_size(bp);
  3307. val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
  3308. if (val)
  3309. bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
  3310. macb_dma_desc_get_size(bp);
  3311. }
  3312. mac = of_get_mac_address(np);
  3313. if (mac) {
  3314. ether_addr_copy(bp->dev->dev_addr, mac);
  3315. } else {
  3316. err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
  3317. if (err) {
  3318. if (err == -EPROBE_DEFER)
  3319. goto err_out_free_netdev;
  3320. macb_get_hwaddr(bp);
  3321. }
  3322. }
  3323. err = of_get_phy_mode(np);
  3324. if (err < 0) {
  3325. pdata = dev_get_platdata(&pdev->dev);
  3326. if (pdata && pdata->is_rmii)
  3327. bp->phy_interface = PHY_INTERFACE_MODE_RMII;
  3328. else
  3329. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  3330. } else {
  3331. bp->phy_interface = err;
  3332. }
  3333. /* IP specific init */
  3334. err = init(pdev);
  3335. if (err)
  3336. goto err_out_free_netdev;
  3337. err = macb_mii_init(bp);
  3338. if (err)
  3339. goto err_out_free_netdev;
  3340. phydev = dev->phydev;
  3341. netif_carrier_off(dev);
  3342. err = register_netdev(dev);
  3343. if (err) {
  3344. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  3345. goto err_out_unregister_mdio;
  3346. }
  3347. tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
  3348. (unsigned long)bp);
  3349. phy_attached_info(phydev);
  3350. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  3351. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  3352. dev->base_addr, dev->irq, dev->dev_addr);
  3353. return 0;
  3354. err_out_unregister_mdio:
  3355. phy_disconnect(dev->phydev);
  3356. mdiobus_unregister(bp->mii_bus);
  3357. of_node_put(bp->phy_node);
  3358. if (np && of_phy_is_fixed_link(np))
  3359. of_phy_deregister_fixed_link(np);
  3360. mdiobus_free(bp->mii_bus);
  3361. err_out_free_netdev:
  3362. free_netdev(dev);
  3363. err_disable_clocks:
  3364. clk_disable_unprepare(tx_clk);
  3365. clk_disable_unprepare(hclk);
  3366. clk_disable_unprepare(pclk);
  3367. clk_disable_unprepare(rx_clk);
  3368. return err;
  3369. }
  3370. static int macb_remove(struct platform_device *pdev)
  3371. {
  3372. struct net_device *dev;
  3373. struct macb *bp;
  3374. struct device_node *np = pdev->dev.of_node;
  3375. dev = platform_get_drvdata(pdev);
  3376. if (dev) {
  3377. bp = netdev_priv(dev);
  3378. if (dev->phydev)
  3379. phy_disconnect(dev->phydev);
  3380. mdiobus_unregister(bp->mii_bus);
  3381. if (np && of_phy_is_fixed_link(np))
  3382. of_phy_deregister_fixed_link(np);
  3383. dev->phydev = NULL;
  3384. mdiobus_free(bp->mii_bus);
  3385. unregister_netdev(dev);
  3386. clk_disable_unprepare(bp->tx_clk);
  3387. clk_disable_unprepare(bp->hclk);
  3388. clk_disable_unprepare(bp->pclk);
  3389. clk_disable_unprepare(bp->rx_clk);
  3390. of_node_put(bp->phy_node);
  3391. free_netdev(dev);
  3392. }
  3393. return 0;
  3394. }
  3395. static int __maybe_unused macb_suspend(struct device *dev)
  3396. {
  3397. struct platform_device *pdev = to_platform_device(dev);
  3398. struct net_device *netdev = platform_get_drvdata(pdev);
  3399. struct macb *bp = netdev_priv(netdev);
  3400. netif_carrier_off(netdev);
  3401. netif_device_detach(netdev);
  3402. if (bp->wol & MACB_WOL_ENABLED) {
  3403. macb_writel(bp, IER, MACB_BIT(WOL));
  3404. macb_writel(bp, WOL, MACB_BIT(MAG));
  3405. enable_irq_wake(bp->queues[0].irq);
  3406. } else {
  3407. clk_disable_unprepare(bp->tx_clk);
  3408. clk_disable_unprepare(bp->hclk);
  3409. clk_disable_unprepare(bp->pclk);
  3410. clk_disable_unprepare(bp->rx_clk);
  3411. }
  3412. return 0;
  3413. }
  3414. static int __maybe_unused macb_resume(struct device *dev)
  3415. {
  3416. struct platform_device *pdev = to_platform_device(dev);
  3417. struct net_device *netdev = platform_get_drvdata(pdev);
  3418. struct macb *bp = netdev_priv(netdev);
  3419. if (bp->wol & MACB_WOL_ENABLED) {
  3420. macb_writel(bp, IDR, MACB_BIT(WOL));
  3421. macb_writel(bp, WOL, 0);
  3422. disable_irq_wake(bp->queues[0].irq);
  3423. } else {
  3424. clk_prepare_enable(bp->pclk);
  3425. clk_prepare_enable(bp->hclk);
  3426. clk_prepare_enable(bp->tx_clk);
  3427. clk_prepare_enable(bp->rx_clk);
  3428. }
  3429. netif_device_attach(netdev);
  3430. return 0;
  3431. }
  3432. static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
  3433. static struct platform_driver macb_driver = {
  3434. .probe = macb_probe,
  3435. .remove = macb_remove,
  3436. .driver = {
  3437. .name = "macb",
  3438. .of_match_table = of_match_ptr(macb_dt_ids),
  3439. .pm = &macb_pm_ops,
  3440. },
  3441. };
  3442. module_platform_driver(macb_driver);
  3443. MODULE_LICENSE("GPL");
  3444. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  3445. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  3446. MODULE_ALIAS("platform:macb");