xgmac.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. /*
  2. * Copyright 2010-2011 Calxeda, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/circ_buf.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if.h>
  26. #include <linux/crc32.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/slab.h>
  29. /* XGMAC Register definitions */
  30. #define XGMAC_CONTROL 0x00000000 /* MAC Configuration */
  31. #define XGMAC_FRAME_FILTER 0x00000004 /* MAC Frame Filter */
  32. #define XGMAC_FLOW_CTRL 0x00000018 /* MAC Flow Control */
  33. #define XGMAC_VLAN_TAG 0x0000001C /* VLAN Tags */
  34. #define XGMAC_VERSION 0x00000020 /* Version */
  35. #define XGMAC_VLAN_INCL 0x00000024 /* VLAN tag for tx frames */
  36. #define XGMAC_LPI_CTRL 0x00000028 /* LPI Control and Status */
  37. #define XGMAC_LPI_TIMER 0x0000002C /* LPI Timers Control */
  38. #define XGMAC_TX_PACE 0x00000030 /* Transmit Pace and Stretch */
  39. #define XGMAC_VLAN_HASH 0x00000034 /* VLAN Hash Table */
  40. #define XGMAC_DEBUG 0x00000038 /* Debug */
  41. #define XGMAC_INT_STAT 0x0000003C /* Interrupt and Control */
  42. #define XGMAC_ADDR_HIGH(reg) (0x00000040 + ((reg) * 8))
  43. #define XGMAC_ADDR_LOW(reg) (0x00000044 + ((reg) * 8))
  44. #define XGMAC_HASH(n) (0x00000300 + (n) * 4) /* HASH table regs */
  45. #define XGMAC_NUM_HASH 16
  46. #define XGMAC_OMR 0x00000400
  47. #define XGMAC_REMOTE_WAKE 0x00000700 /* Remote Wake-Up Frm Filter */
  48. #define XGMAC_PMT 0x00000704 /* PMT Control and Status */
  49. #define XGMAC_MMC_CTRL 0x00000800 /* XGMAC MMC Control */
  50. #define XGMAC_MMC_INTR_RX 0x00000804 /* Recieve Interrupt */
  51. #define XGMAC_MMC_INTR_TX 0x00000808 /* Transmit Interrupt */
  52. #define XGMAC_MMC_INTR_MASK_RX 0x0000080c /* Recieve Interrupt Mask */
  53. #define XGMAC_MMC_INTR_MASK_TX 0x00000810 /* Transmit Interrupt Mask */
  54. /* Hardware TX Statistics Counters */
  55. #define XGMAC_MMC_TXOCTET_GB_LO 0x00000814
  56. #define XGMAC_MMC_TXOCTET_GB_HI 0x00000818
  57. #define XGMAC_MMC_TXFRAME_GB_LO 0x0000081C
  58. #define XGMAC_MMC_TXFRAME_GB_HI 0x00000820
  59. #define XGMAC_MMC_TXBCFRAME_G 0x00000824
  60. #define XGMAC_MMC_TXMCFRAME_G 0x0000082C
  61. #define XGMAC_MMC_TXUCFRAME_GB 0x00000864
  62. #define XGMAC_MMC_TXMCFRAME_GB 0x0000086C
  63. #define XGMAC_MMC_TXBCFRAME_GB 0x00000874
  64. #define XGMAC_MMC_TXUNDERFLOW 0x0000087C
  65. #define XGMAC_MMC_TXOCTET_G_LO 0x00000884
  66. #define XGMAC_MMC_TXOCTET_G_HI 0x00000888
  67. #define XGMAC_MMC_TXFRAME_G_LO 0x0000088C
  68. #define XGMAC_MMC_TXFRAME_G_HI 0x00000890
  69. #define XGMAC_MMC_TXPAUSEFRAME 0x00000894
  70. #define XGMAC_MMC_TXVLANFRAME 0x0000089C
  71. /* Hardware RX Statistics Counters */
  72. #define XGMAC_MMC_RXFRAME_GB_LO 0x00000900
  73. #define XGMAC_MMC_RXFRAME_GB_HI 0x00000904
  74. #define XGMAC_MMC_RXOCTET_GB_LO 0x00000908
  75. #define XGMAC_MMC_RXOCTET_GB_HI 0x0000090C
  76. #define XGMAC_MMC_RXOCTET_G_LO 0x00000910
  77. #define XGMAC_MMC_RXOCTET_G_HI 0x00000914
  78. #define XGMAC_MMC_RXBCFRAME_G 0x00000918
  79. #define XGMAC_MMC_RXMCFRAME_G 0x00000920
  80. #define XGMAC_MMC_RXCRCERR 0x00000928
  81. #define XGMAC_MMC_RXRUNT 0x00000930
  82. #define XGMAC_MMC_RXJABBER 0x00000934
  83. #define XGMAC_MMC_RXUCFRAME_G 0x00000970
  84. #define XGMAC_MMC_RXLENGTHERR 0x00000978
  85. #define XGMAC_MMC_RXPAUSEFRAME 0x00000988
  86. #define XGMAC_MMC_RXOVERFLOW 0x00000990
  87. #define XGMAC_MMC_RXVLANFRAME 0x00000998
  88. #define XGMAC_MMC_RXWATCHDOG 0x000009a0
  89. /* DMA Control and Status Registers */
  90. #define XGMAC_DMA_BUS_MODE 0x00000f00 /* Bus Mode */
  91. #define XGMAC_DMA_TX_POLL 0x00000f04 /* Transmit Poll Demand */
  92. #define XGMAC_DMA_RX_POLL 0x00000f08 /* Received Poll Demand */
  93. #define XGMAC_DMA_RX_BASE_ADDR 0x00000f0c /* Receive List Base */
  94. #define XGMAC_DMA_TX_BASE_ADDR 0x00000f10 /* Transmit List Base */
  95. #define XGMAC_DMA_STATUS 0x00000f14 /* Status Register */
  96. #define XGMAC_DMA_CONTROL 0x00000f18 /* Ctrl (Operational Mode) */
  97. #define XGMAC_DMA_INTR_ENA 0x00000f1c /* Interrupt Enable */
  98. #define XGMAC_DMA_MISS_FRAME_CTR 0x00000f20 /* Missed Frame Counter */
  99. #define XGMAC_DMA_RI_WDOG_TIMER 0x00000f24 /* RX Intr Watchdog Timer */
  100. #define XGMAC_DMA_AXI_BUS 0x00000f28 /* AXI Bus Mode */
  101. #define XGMAC_DMA_AXI_STATUS 0x00000f2C /* AXI Status */
  102. #define XGMAC_DMA_HW_FEATURE 0x00000f58 /* Enabled Hardware Features */
  103. #define XGMAC_ADDR_AE 0x80000000
  104. #define XGMAC_MAX_FILTER_ADDR 31
  105. /* PMT Control and Status */
  106. #define XGMAC_PMT_POINTER_RESET 0x80000000
  107. #define XGMAC_PMT_GLBL_UNICAST 0x00000200
  108. #define XGMAC_PMT_WAKEUP_RX_FRM 0x00000040
  109. #define XGMAC_PMT_MAGIC_PKT 0x00000020
  110. #define XGMAC_PMT_WAKEUP_FRM_EN 0x00000004
  111. #define XGMAC_PMT_MAGIC_PKT_EN 0x00000002
  112. #define XGMAC_PMT_POWERDOWN 0x00000001
  113. #define XGMAC_CONTROL_SPD 0x40000000 /* Speed control */
  114. #define XGMAC_CONTROL_SPD_MASK 0x60000000
  115. #define XGMAC_CONTROL_SPD_1G 0x60000000
  116. #define XGMAC_CONTROL_SPD_2_5G 0x40000000
  117. #define XGMAC_CONTROL_SPD_10G 0x00000000
  118. #define XGMAC_CONTROL_SARC 0x10000000 /* Source Addr Insert/Replace */
  119. #define XGMAC_CONTROL_SARK_MASK 0x18000000
  120. #define XGMAC_CONTROL_CAR 0x04000000 /* CRC Addition/Replacement */
  121. #define XGMAC_CONTROL_CAR_MASK 0x06000000
  122. #define XGMAC_CONTROL_DP 0x01000000 /* Disable Padding */
  123. #define XGMAC_CONTROL_WD 0x00800000 /* Disable Watchdog on rx */
  124. #define XGMAC_CONTROL_JD 0x00400000 /* Jabber disable */
  125. #define XGMAC_CONTROL_JE 0x00100000 /* Jumbo frame */
  126. #define XGMAC_CONTROL_LM 0x00001000 /* Loop-back mode */
  127. #define XGMAC_CONTROL_IPC 0x00000400 /* Checksum Offload */
  128. #define XGMAC_CONTROL_ACS 0x00000080 /* Automatic Pad/FCS Strip */
  129. #define XGMAC_CONTROL_DDIC 0x00000010 /* Disable Deficit Idle Count */
  130. #define XGMAC_CONTROL_TE 0x00000008 /* Transmitter Enable */
  131. #define XGMAC_CONTROL_RE 0x00000004 /* Receiver Enable */
  132. /* XGMAC Frame Filter defines */
  133. #define XGMAC_FRAME_FILTER_PR 0x00000001 /* Promiscuous Mode */
  134. #define XGMAC_FRAME_FILTER_HUC 0x00000002 /* Hash Unicast */
  135. #define XGMAC_FRAME_FILTER_HMC 0x00000004 /* Hash Multicast */
  136. #define XGMAC_FRAME_FILTER_DAIF 0x00000008 /* DA Inverse Filtering */
  137. #define XGMAC_FRAME_FILTER_PM 0x00000010 /* Pass all multicast */
  138. #define XGMAC_FRAME_FILTER_DBF 0x00000020 /* Disable Broadcast frames */
  139. #define XGMAC_FRAME_FILTER_SAIF 0x00000100 /* Inverse Filtering */
  140. #define XGMAC_FRAME_FILTER_SAF 0x00000200 /* Source Address Filter */
  141. #define XGMAC_FRAME_FILTER_HPF 0x00000400 /* Hash or perfect Filter */
  142. #define XGMAC_FRAME_FILTER_VHF 0x00000800 /* VLAN Hash Filter */
  143. #define XGMAC_FRAME_FILTER_VPF 0x00001000 /* VLAN Perfect Filter */
  144. #define XGMAC_FRAME_FILTER_RA 0x80000000 /* Receive all mode */
  145. /* XGMAC FLOW CTRL defines */
  146. #define XGMAC_FLOW_CTRL_PT_MASK 0xffff0000 /* Pause Time Mask */
  147. #define XGMAC_FLOW_CTRL_PT_SHIFT 16
  148. #define XGMAC_FLOW_CTRL_DZQP 0x00000080 /* Disable Zero-Quanta Phase */
  149. #define XGMAC_FLOW_CTRL_PLT 0x00000020 /* Pause Low Threshhold */
  150. #define XGMAC_FLOW_CTRL_PLT_MASK 0x00000030 /* PLT MASK */
  151. #define XGMAC_FLOW_CTRL_UP 0x00000008 /* Unicast Pause Frame Detect */
  152. #define XGMAC_FLOW_CTRL_RFE 0x00000004 /* Rx Flow Control Enable */
  153. #define XGMAC_FLOW_CTRL_TFE 0x00000002 /* Tx Flow Control Enable */
  154. #define XGMAC_FLOW_CTRL_FCB_BPA 0x00000001 /* Flow Control Busy ... */
  155. /* XGMAC_INT_STAT reg */
  156. #define XGMAC_INT_STAT_PMT 0x0080 /* PMT Interrupt Status */
  157. #define XGMAC_INT_STAT_LPI 0x0040 /* LPI Interrupt Status */
  158. /* DMA Bus Mode register defines */
  159. #define DMA_BUS_MODE_SFT_RESET 0x00000001 /* Software Reset */
  160. #define DMA_BUS_MODE_DSL_MASK 0x0000007c /* Descriptor Skip Length */
  161. #define DMA_BUS_MODE_DSL_SHIFT 2 /* (in DWORDS) */
  162. #define DMA_BUS_MODE_ATDS 0x00000080 /* Alternate Descriptor Size */
  163. /* Programmable burst length */
  164. #define DMA_BUS_MODE_PBL_MASK 0x00003f00 /* Programmable Burst Len */
  165. #define DMA_BUS_MODE_PBL_SHIFT 8
  166. #define DMA_BUS_MODE_FB 0x00010000 /* Fixed burst */
  167. #define DMA_BUS_MODE_RPBL_MASK 0x003e0000 /* Rx-Programmable Burst Len */
  168. #define DMA_BUS_MODE_RPBL_SHIFT 17
  169. #define DMA_BUS_MODE_USP 0x00800000
  170. #define DMA_BUS_MODE_8PBL 0x01000000
  171. #define DMA_BUS_MODE_AAL 0x02000000
  172. /* DMA Bus Mode register defines */
  173. #define DMA_BUS_PR_RATIO_MASK 0x0000c000 /* Rx/Tx priority ratio */
  174. #define DMA_BUS_PR_RATIO_SHIFT 14
  175. #define DMA_BUS_FB 0x00010000 /* Fixed Burst */
  176. /* DMA Control register defines */
  177. #define DMA_CONTROL_ST 0x00002000 /* Start/Stop Transmission */
  178. #define DMA_CONTROL_SR 0x00000002 /* Start/Stop Receive */
  179. #define DMA_CONTROL_DFF 0x01000000 /* Disable flush of rx frames */
  180. /* DMA Normal interrupt */
  181. #define DMA_INTR_ENA_NIE 0x00010000 /* Normal Summary */
  182. #define DMA_INTR_ENA_AIE 0x00008000 /* Abnormal Summary */
  183. #define DMA_INTR_ENA_ERE 0x00004000 /* Early Receive */
  184. #define DMA_INTR_ENA_FBE 0x00002000 /* Fatal Bus Error */
  185. #define DMA_INTR_ENA_ETE 0x00000400 /* Early Transmit */
  186. #define DMA_INTR_ENA_RWE 0x00000200 /* Receive Watchdog */
  187. #define DMA_INTR_ENA_RSE 0x00000100 /* Receive Stopped */
  188. #define DMA_INTR_ENA_RUE 0x00000080 /* Receive Buffer Unavailable */
  189. #define DMA_INTR_ENA_RIE 0x00000040 /* Receive Interrupt */
  190. #define DMA_INTR_ENA_UNE 0x00000020 /* Tx Underflow */
  191. #define DMA_INTR_ENA_OVE 0x00000010 /* Receive Overflow */
  192. #define DMA_INTR_ENA_TJE 0x00000008 /* Transmit Jabber */
  193. #define DMA_INTR_ENA_TUE 0x00000004 /* Transmit Buffer Unavail */
  194. #define DMA_INTR_ENA_TSE 0x00000002 /* Transmit Stopped */
  195. #define DMA_INTR_ENA_TIE 0x00000001 /* Transmit Interrupt */
  196. #define DMA_INTR_NORMAL (DMA_INTR_ENA_NIE | DMA_INTR_ENA_RIE | \
  197. DMA_INTR_ENA_TUE)
  198. #define DMA_INTR_ABNORMAL (DMA_INTR_ENA_AIE | DMA_INTR_ENA_FBE | \
  199. DMA_INTR_ENA_RWE | DMA_INTR_ENA_RSE | \
  200. DMA_INTR_ENA_RUE | DMA_INTR_ENA_UNE | \
  201. DMA_INTR_ENA_OVE | DMA_INTR_ENA_TJE | \
  202. DMA_INTR_ENA_TSE)
  203. /* DMA default interrupt mask */
  204. #define DMA_INTR_DEFAULT_MASK (DMA_INTR_NORMAL | DMA_INTR_ABNORMAL)
  205. /* DMA Status register defines */
  206. #define DMA_STATUS_GMI 0x08000000 /* MMC interrupt */
  207. #define DMA_STATUS_GLI 0x04000000 /* GMAC Line interface int */
  208. #define DMA_STATUS_EB_MASK 0x00380000 /* Error Bits Mask */
  209. #define DMA_STATUS_EB_TX_ABORT 0x00080000 /* Error Bits - TX Abort */
  210. #define DMA_STATUS_EB_RX_ABORT 0x00100000 /* Error Bits - RX Abort */
  211. #define DMA_STATUS_TS_MASK 0x00700000 /* Transmit Process State */
  212. #define DMA_STATUS_TS_SHIFT 20
  213. #define DMA_STATUS_RS_MASK 0x000e0000 /* Receive Process State */
  214. #define DMA_STATUS_RS_SHIFT 17
  215. #define DMA_STATUS_NIS 0x00010000 /* Normal Interrupt Summary */
  216. #define DMA_STATUS_AIS 0x00008000 /* Abnormal Interrupt Summary */
  217. #define DMA_STATUS_ERI 0x00004000 /* Early Receive Interrupt */
  218. #define DMA_STATUS_FBI 0x00002000 /* Fatal Bus Error Interrupt */
  219. #define DMA_STATUS_ETI 0x00000400 /* Early Transmit Interrupt */
  220. #define DMA_STATUS_RWT 0x00000200 /* Receive Watchdog Timeout */
  221. #define DMA_STATUS_RPS 0x00000100 /* Receive Process Stopped */
  222. #define DMA_STATUS_RU 0x00000080 /* Receive Buffer Unavailable */
  223. #define DMA_STATUS_RI 0x00000040 /* Receive Interrupt */
  224. #define DMA_STATUS_UNF 0x00000020 /* Transmit Underflow */
  225. #define DMA_STATUS_OVF 0x00000010 /* Receive Overflow */
  226. #define DMA_STATUS_TJT 0x00000008 /* Transmit Jabber Timeout */
  227. #define DMA_STATUS_TU 0x00000004 /* Transmit Buffer Unavail */
  228. #define DMA_STATUS_TPS 0x00000002 /* Transmit Process Stopped */
  229. #define DMA_STATUS_TI 0x00000001 /* Transmit Interrupt */
  230. /* Common MAC defines */
  231. #define MAC_ENABLE_TX 0x00000008 /* Transmitter Enable */
  232. #define MAC_ENABLE_RX 0x00000004 /* Receiver Enable */
  233. /* XGMAC Operation Mode Register */
  234. #define XGMAC_OMR_TSF 0x00200000 /* TX FIFO Store and Forward */
  235. #define XGMAC_OMR_FTF 0x00100000 /* Flush Transmit FIFO */
  236. #define XGMAC_OMR_TTC 0x00020000 /* Transmit Threshhold Ctrl */
  237. #define XGMAC_OMR_TTC_MASK 0x00030000
  238. #define XGMAC_OMR_RFD 0x00006000 /* FC Deactivation Threshhold */
  239. #define XGMAC_OMR_RFD_MASK 0x00007000 /* FC Deact Threshhold MASK */
  240. #define XGMAC_OMR_RFA 0x00000600 /* FC Activation Threshhold */
  241. #define XGMAC_OMR_RFA_MASK 0x00000E00 /* FC Act Threshhold MASK */
  242. #define XGMAC_OMR_EFC 0x00000100 /* Enable Hardware FC */
  243. #define XGMAC_OMR_FEF 0x00000080 /* Forward Error Frames */
  244. #define XGMAC_OMR_DT 0x00000040 /* Drop TCP/IP csum Errors */
  245. #define XGMAC_OMR_RSF 0x00000020 /* RX FIFO Store and Forward */
  246. #define XGMAC_OMR_RTC 0x00000010 /* RX Threshhold Ctrl */
  247. #define XGMAC_OMR_RTC_MASK 0x00000018 /* RX Threshhold Ctrl MASK */
  248. /* XGMAC HW Features Register */
  249. #define DMA_HW_FEAT_TXCOESEL 0x00010000 /* TX Checksum offload */
  250. #define XGMAC_MMC_CTRL_CNT_FRZ 0x00000008
  251. /* XGMAC Descriptor Defines */
  252. #define MAX_DESC_BUF_SZ (0x2000 - 8)
  253. #define RXDESC_EXT_STATUS 0x00000001
  254. #define RXDESC_CRC_ERR 0x00000002
  255. #define RXDESC_RX_ERR 0x00000008
  256. #define RXDESC_RX_WDOG 0x00000010
  257. #define RXDESC_FRAME_TYPE 0x00000020
  258. #define RXDESC_GIANT_FRAME 0x00000080
  259. #define RXDESC_LAST_SEG 0x00000100
  260. #define RXDESC_FIRST_SEG 0x00000200
  261. #define RXDESC_VLAN_FRAME 0x00000400
  262. #define RXDESC_OVERFLOW_ERR 0x00000800
  263. #define RXDESC_LENGTH_ERR 0x00001000
  264. #define RXDESC_SA_FILTER_FAIL 0x00002000
  265. #define RXDESC_DESCRIPTOR_ERR 0x00004000
  266. #define RXDESC_ERROR_SUMMARY 0x00008000
  267. #define RXDESC_FRAME_LEN_OFFSET 16
  268. #define RXDESC_FRAME_LEN_MASK 0x3fff0000
  269. #define RXDESC_DA_FILTER_FAIL 0x40000000
  270. #define RXDESC1_END_RING 0x00008000
  271. #define RXDESC_IP_PAYLOAD_MASK 0x00000003
  272. #define RXDESC_IP_PAYLOAD_UDP 0x00000001
  273. #define RXDESC_IP_PAYLOAD_TCP 0x00000002
  274. #define RXDESC_IP_PAYLOAD_ICMP 0x00000003
  275. #define RXDESC_IP_HEADER_ERR 0x00000008
  276. #define RXDESC_IP_PAYLOAD_ERR 0x00000010
  277. #define RXDESC_IPV4_PACKET 0x00000040
  278. #define RXDESC_IPV6_PACKET 0x00000080
  279. #define TXDESC_UNDERFLOW_ERR 0x00000001
  280. #define TXDESC_JABBER_TIMEOUT 0x00000002
  281. #define TXDESC_LOCAL_FAULT 0x00000004
  282. #define TXDESC_REMOTE_FAULT 0x00000008
  283. #define TXDESC_VLAN_FRAME 0x00000010
  284. #define TXDESC_FRAME_FLUSHED 0x00000020
  285. #define TXDESC_IP_HEADER_ERR 0x00000040
  286. #define TXDESC_PAYLOAD_CSUM_ERR 0x00000080
  287. #define TXDESC_ERROR_SUMMARY 0x00008000
  288. #define TXDESC_SA_CTRL_INSERT 0x00040000
  289. #define TXDESC_SA_CTRL_REPLACE 0x00080000
  290. #define TXDESC_2ND_ADDR_CHAINED 0x00100000
  291. #define TXDESC_END_RING 0x00200000
  292. #define TXDESC_CSUM_IP 0x00400000
  293. #define TXDESC_CSUM_IP_PAYLD 0x00800000
  294. #define TXDESC_CSUM_ALL 0x00C00000
  295. #define TXDESC_CRC_EN_REPLACE 0x01000000
  296. #define TXDESC_CRC_EN_APPEND 0x02000000
  297. #define TXDESC_DISABLE_PAD 0x04000000
  298. #define TXDESC_FIRST_SEG 0x10000000
  299. #define TXDESC_LAST_SEG 0x20000000
  300. #define TXDESC_INTERRUPT 0x40000000
  301. #define DESC_OWN 0x80000000
  302. #define DESC_BUFFER1_SZ_MASK 0x00001fff
  303. #define DESC_BUFFER2_SZ_MASK 0x1fff0000
  304. #define DESC_BUFFER2_SZ_OFFSET 16
  305. struct xgmac_dma_desc {
  306. __le32 flags;
  307. __le32 buf_size;
  308. __le32 buf1_addr; /* Buffer 1 Address Pointer */
  309. __le32 buf2_addr; /* Buffer 2 Address Pointer */
  310. __le32 ext_status;
  311. __le32 res[3];
  312. };
  313. struct xgmac_extra_stats {
  314. /* Transmit errors */
  315. unsigned long tx_jabber;
  316. unsigned long tx_frame_flushed;
  317. unsigned long tx_payload_error;
  318. unsigned long tx_ip_header_error;
  319. unsigned long tx_local_fault;
  320. unsigned long tx_remote_fault;
  321. /* Receive errors */
  322. unsigned long rx_watchdog;
  323. unsigned long rx_da_filter_fail;
  324. unsigned long rx_sa_filter_fail;
  325. unsigned long rx_payload_error;
  326. unsigned long rx_ip_header_error;
  327. /* Tx/Rx IRQ errors */
  328. unsigned long tx_undeflow;
  329. unsigned long tx_process_stopped;
  330. unsigned long rx_buf_unav;
  331. unsigned long rx_process_stopped;
  332. unsigned long tx_early;
  333. unsigned long fatal_bus_error;
  334. };
  335. struct xgmac_priv {
  336. struct xgmac_dma_desc *dma_rx;
  337. struct sk_buff **rx_skbuff;
  338. unsigned int rx_tail;
  339. unsigned int rx_head;
  340. struct xgmac_dma_desc *dma_tx;
  341. struct sk_buff **tx_skbuff;
  342. unsigned int tx_head;
  343. unsigned int tx_tail;
  344. void __iomem *base;
  345. struct sk_buff_head rx_recycle;
  346. unsigned int dma_buf_sz;
  347. dma_addr_t dma_rx_phy;
  348. dma_addr_t dma_tx_phy;
  349. struct net_device *dev;
  350. struct device *device;
  351. struct napi_struct napi;
  352. struct xgmac_extra_stats xstats;
  353. spinlock_t stats_lock;
  354. int pmt_irq;
  355. char rx_pause;
  356. char tx_pause;
  357. int wolopts;
  358. };
  359. /* XGMAC Configuration Settings */
  360. #define MAX_MTU 9000
  361. #define PAUSE_TIME 0x400
  362. #define DMA_RX_RING_SZ 256
  363. #define DMA_TX_RING_SZ 128
  364. /* minimum number of free TX descriptors required to wake up TX process */
  365. #define TX_THRESH (DMA_TX_RING_SZ/4)
  366. /* DMA descriptor ring helpers */
  367. #define dma_ring_incr(n, s) (((n) + 1) & ((s) - 1))
  368. #define dma_ring_space(h, t, s) CIRC_SPACE(h, t, s)
  369. #define dma_ring_cnt(h, t, s) CIRC_CNT(h, t, s)
  370. /* XGMAC Descriptor Access Helpers */
  371. static inline void desc_set_buf_len(struct xgmac_dma_desc *p, u32 buf_sz)
  372. {
  373. if (buf_sz > MAX_DESC_BUF_SZ)
  374. p->buf_size = cpu_to_le32(MAX_DESC_BUF_SZ |
  375. (buf_sz - MAX_DESC_BUF_SZ) << DESC_BUFFER2_SZ_OFFSET);
  376. else
  377. p->buf_size = cpu_to_le32(buf_sz);
  378. }
  379. static inline int desc_get_buf_len(struct xgmac_dma_desc *p)
  380. {
  381. u32 len = cpu_to_le32(p->flags);
  382. return (len & DESC_BUFFER1_SZ_MASK) +
  383. ((len & DESC_BUFFER2_SZ_MASK) >> DESC_BUFFER2_SZ_OFFSET);
  384. }
  385. static inline void desc_init_rx_desc(struct xgmac_dma_desc *p, int ring_size,
  386. int buf_sz)
  387. {
  388. struct xgmac_dma_desc *end = p + ring_size - 1;
  389. memset(p, 0, sizeof(*p) * ring_size);
  390. for (; p <= end; p++)
  391. desc_set_buf_len(p, buf_sz);
  392. end->buf_size |= cpu_to_le32(RXDESC1_END_RING);
  393. }
  394. static inline void desc_init_tx_desc(struct xgmac_dma_desc *p, u32 ring_size)
  395. {
  396. memset(p, 0, sizeof(*p) * ring_size);
  397. p[ring_size - 1].flags = cpu_to_le32(TXDESC_END_RING);
  398. }
  399. static inline int desc_get_owner(struct xgmac_dma_desc *p)
  400. {
  401. return le32_to_cpu(p->flags) & DESC_OWN;
  402. }
  403. static inline void desc_set_rx_owner(struct xgmac_dma_desc *p)
  404. {
  405. /* Clear all fields and set the owner */
  406. p->flags = cpu_to_le32(DESC_OWN);
  407. }
  408. static inline void desc_set_tx_owner(struct xgmac_dma_desc *p, u32 flags)
  409. {
  410. u32 tmpflags = le32_to_cpu(p->flags);
  411. tmpflags &= TXDESC_END_RING;
  412. tmpflags |= flags | DESC_OWN;
  413. p->flags = cpu_to_le32(tmpflags);
  414. }
  415. static inline int desc_get_tx_ls(struct xgmac_dma_desc *p)
  416. {
  417. return le32_to_cpu(p->flags) & TXDESC_LAST_SEG;
  418. }
  419. static inline u32 desc_get_buf_addr(struct xgmac_dma_desc *p)
  420. {
  421. return le32_to_cpu(p->buf1_addr);
  422. }
  423. static inline void desc_set_buf_addr(struct xgmac_dma_desc *p,
  424. u32 paddr, int len)
  425. {
  426. p->buf1_addr = cpu_to_le32(paddr);
  427. if (len > MAX_DESC_BUF_SZ)
  428. p->buf2_addr = cpu_to_le32(paddr + MAX_DESC_BUF_SZ);
  429. }
  430. static inline void desc_set_buf_addr_and_size(struct xgmac_dma_desc *p,
  431. u32 paddr, int len)
  432. {
  433. desc_set_buf_len(p, len);
  434. desc_set_buf_addr(p, paddr, len);
  435. }
  436. static inline int desc_get_rx_frame_len(struct xgmac_dma_desc *p)
  437. {
  438. u32 data = le32_to_cpu(p->flags);
  439. u32 len = (data & RXDESC_FRAME_LEN_MASK) >> RXDESC_FRAME_LEN_OFFSET;
  440. if (data & RXDESC_FRAME_TYPE)
  441. len -= ETH_FCS_LEN;
  442. return len;
  443. }
  444. static void xgmac_dma_flush_tx_fifo(void __iomem *ioaddr)
  445. {
  446. int timeout = 1000;
  447. u32 reg = readl(ioaddr + XGMAC_OMR);
  448. writel(reg | XGMAC_OMR_FTF, ioaddr + XGMAC_OMR);
  449. while ((timeout-- > 0) && readl(ioaddr + XGMAC_OMR) & XGMAC_OMR_FTF)
  450. udelay(1);
  451. }
  452. static int desc_get_tx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  453. {
  454. struct xgmac_extra_stats *x = &priv->xstats;
  455. u32 status = le32_to_cpu(p->flags);
  456. if (!(status & TXDESC_ERROR_SUMMARY))
  457. return 0;
  458. netdev_dbg(priv->dev, "tx desc error = 0x%08x\n", status);
  459. if (status & TXDESC_JABBER_TIMEOUT)
  460. x->tx_jabber++;
  461. if (status & TXDESC_FRAME_FLUSHED)
  462. x->tx_frame_flushed++;
  463. if (status & TXDESC_UNDERFLOW_ERR)
  464. xgmac_dma_flush_tx_fifo(priv->base);
  465. if (status & TXDESC_IP_HEADER_ERR)
  466. x->tx_ip_header_error++;
  467. if (status & TXDESC_LOCAL_FAULT)
  468. x->tx_local_fault++;
  469. if (status & TXDESC_REMOTE_FAULT)
  470. x->tx_remote_fault++;
  471. if (status & TXDESC_PAYLOAD_CSUM_ERR)
  472. x->tx_payload_error++;
  473. return -1;
  474. }
  475. static int desc_get_rx_status(struct xgmac_priv *priv, struct xgmac_dma_desc *p)
  476. {
  477. struct xgmac_extra_stats *x = &priv->xstats;
  478. int ret = CHECKSUM_UNNECESSARY;
  479. u32 status = le32_to_cpu(p->flags);
  480. u32 ext_status = le32_to_cpu(p->ext_status);
  481. if (status & RXDESC_DA_FILTER_FAIL) {
  482. netdev_dbg(priv->dev, "XGMAC RX : Dest Address filter fail\n");
  483. x->rx_da_filter_fail++;
  484. return -1;
  485. }
  486. /* All frames should fit into a single buffer */
  487. if (!(status & RXDESC_FIRST_SEG) || !(status & RXDESC_LAST_SEG))
  488. return -1;
  489. /* Check if packet has checksum already */
  490. if ((status & RXDESC_FRAME_TYPE) && (status & RXDESC_EXT_STATUS) &&
  491. !(ext_status & RXDESC_IP_PAYLOAD_MASK))
  492. ret = CHECKSUM_NONE;
  493. netdev_dbg(priv->dev, "rx status - frame type=%d, csum = %d, ext stat %08x\n",
  494. (status & RXDESC_FRAME_TYPE) ? 1 : 0, ret, ext_status);
  495. if (!(status & RXDESC_ERROR_SUMMARY))
  496. return ret;
  497. /* Handle any errors */
  498. if (status & (RXDESC_DESCRIPTOR_ERR | RXDESC_OVERFLOW_ERR |
  499. RXDESC_GIANT_FRAME | RXDESC_LENGTH_ERR | RXDESC_CRC_ERR))
  500. return -1;
  501. if (status & RXDESC_EXT_STATUS) {
  502. if (ext_status & RXDESC_IP_HEADER_ERR)
  503. x->rx_ip_header_error++;
  504. if (ext_status & RXDESC_IP_PAYLOAD_ERR)
  505. x->rx_payload_error++;
  506. netdev_dbg(priv->dev, "IP checksum error - stat %08x\n",
  507. ext_status);
  508. return CHECKSUM_NONE;
  509. }
  510. return ret;
  511. }
  512. static inline void xgmac_mac_enable(void __iomem *ioaddr)
  513. {
  514. u32 value = readl(ioaddr + XGMAC_CONTROL);
  515. value |= MAC_ENABLE_RX | MAC_ENABLE_TX;
  516. writel(value, ioaddr + XGMAC_CONTROL);
  517. value = readl(ioaddr + XGMAC_DMA_CONTROL);
  518. value |= DMA_CONTROL_ST | DMA_CONTROL_SR;
  519. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  520. }
  521. static inline void xgmac_mac_disable(void __iomem *ioaddr)
  522. {
  523. u32 value = readl(ioaddr + XGMAC_DMA_CONTROL);
  524. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  525. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  526. value = readl(ioaddr + XGMAC_CONTROL);
  527. value &= ~(MAC_ENABLE_TX | MAC_ENABLE_RX);
  528. writel(value, ioaddr + XGMAC_CONTROL);
  529. }
  530. static void xgmac_set_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  531. int num)
  532. {
  533. u32 data;
  534. data = (addr[5] << 8) | addr[4] | (num ? XGMAC_ADDR_AE : 0);
  535. writel(data, ioaddr + XGMAC_ADDR_HIGH(num));
  536. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  537. writel(data, ioaddr + XGMAC_ADDR_LOW(num));
  538. }
  539. static void xgmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  540. int num)
  541. {
  542. u32 hi_addr, lo_addr;
  543. /* Read the MAC address from the hardware */
  544. hi_addr = readl(ioaddr + XGMAC_ADDR_HIGH(num));
  545. lo_addr = readl(ioaddr + XGMAC_ADDR_LOW(num));
  546. /* Extract the MAC address from the high and low words */
  547. addr[0] = lo_addr & 0xff;
  548. addr[1] = (lo_addr >> 8) & 0xff;
  549. addr[2] = (lo_addr >> 16) & 0xff;
  550. addr[3] = (lo_addr >> 24) & 0xff;
  551. addr[4] = hi_addr & 0xff;
  552. addr[5] = (hi_addr >> 8) & 0xff;
  553. }
  554. static int xgmac_set_flow_ctrl(struct xgmac_priv *priv, int rx, int tx)
  555. {
  556. u32 reg;
  557. unsigned int flow = 0;
  558. priv->rx_pause = rx;
  559. priv->tx_pause = tx;
  560. if (rx || tx) {
  561. if (rx)
  562. flow |= XGMAC_FLOW_CTRL_RFE;
  563. if (tx)
  564. flow |= XGMAC_FLOW_CTRL_TFE;
  565. flow |= XGMAC_FLOW_CTRL_PLT | XGMAC_FLOW_CTRL_UP;
  566. flow |= (PAUSE_TIME << XGMAC_FLOW_CTRL_PT_SHIFT);
  567. writel(flow, priv->base + XGMAC_FLOW_CTRL);
  568. reg = readl(priv->base + XGMAC_OMR);
  569. reg |= XGMAC_OMR_EFC;
  570. writel(reg, priv->base + XGMAC_OMR);
  571. } else {
  572. writel(0, priv->base + XGMAC_FLOW_CTRL);
  573. reg = readl(priv->base + XGMAC_OMR);
  574. reg &= ~XGMAC_OMR_EFC;
  575. writel(reg, priv->base + XGMAC_OMR);
  576. }
  577. return 0;
  578. }
  579. static void xgmac_rx_refill(struct xgmac_priv *priv)
  580. {
  581. struct xgmac_dma_desc *p;
  582. dma_addr_t paddr;
  583. while (dma_ring_space(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ) > 1) {
  584. int entry = priv->rx_head;
  585. struct sk_buff *skb;
  586. p = priv->dma_rx + entry;
  587. if (priv->rx_skbuff[entry] != NULL)
  588. continue;
  589. skb = __skb_dequeue(&priv->rx_recycle);
  590. if (skb == NULL)
  591. skb = netdev_alloc_skb(priv->dev, priv->dma_buf_sz);
  592. if (unlikely(skb == NULL))
  593. break;
  594. priv->rx_skbuff[entry] = skb;
  595. paddr = dma_map_single(priv->device, skb->data,
  596. priv->dma_buf_sz, DMA_FROM_DEVICE);
  597. desc_set_buf_addr(p, paddr, priv->dma_buf_sz);
  598. netdev_dbg(priv->dev, "rx ring: head %d, tail %d\n",
  599. priv->rx_head, priv->rx_tail);
  600. priv->rx_head = dma_ring_incr(priv->rx_head, DMA_RX_RING_SZ);
  601. /* Ensure descriptor is in memory before handing to h/w */
  602. wmb();
  603. desc_set_rx_owner(p);
  604. }
  605. }
  606. /**
  607. * init_xgmac_dma_desc_rings - init the RX/TX descriptor rings
  608. * @dev: net device structure
  609. * Description: this function initializes the DMA RX/TX descriptors
  610. * and allocates the socket buffers.
  611. */
  612. static int xgmac_dma_desc_rings_init(struct net_device *dev)
  613. {
  614. struct xgmac_priv *priv = netdev_priv(dev);
  615. unsigned int bfsize;
  616. /* Set the Buffer size according to the MTU;
  617. * indeed, in case of jumbo we need to bump-up the buffer sizes.
  618. */
  619. bfsize = ALIGN(dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN + 64,
  620. 64);
  621. netdev_dbg(priv->dev, "mtu [%d] bfsize [%d]\n", dev->mtu, bfsize);
  622. priv->rx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_RX_RING_SZ,
  623. GFP_KERNEL);
  624. if (!priv->rx_skbuff)
  625. return -ENOMEM;
  626. priv->dma_rx = dma_alloc_coherent(priv->device,
  627. DMA_RX_RING_SZ *
  628. sizeof(struct xgmac_dma_desc),
  629. &priv->dma_rx_phy,
  630. GFP_KERNEL);
  631. if (!priv->dma_rx)
  632. goto err_dma_rx;
  633. priv->tx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_TX_RING_SZ,
  634. GFP_KERNEL);
  635. if (!priv->tx_skbuff)
  636. goto err_tx_skb;
  637. priv->dma_tx = dma_alloc_coherent(priv->device,
  638. DMA_TX_RING_SZ *
  639. sizeof(struct xgmac_dma_desc),
  640. &priv->dma_tx_phy,
  641. GFP_KERNEL);
  642. if (!priv->dma_tx)
  643. goto err_dma_tx;
  644. netdev_dbg(priv->dev, "DMA desc rings: virt addr (Rx %p, "
  645. "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
  646. priv->dma_rx, priv->dma_tx,
  647. (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
  648. priv->rx_tail = 0;
  649. priv->rx_head = 0;
  650. priv->dma_buf_sz = bfsize;
  651. desc_init_rx_desc(priv->dma_rx, DMA_RX_RING_SZ, priv->dma_buf_sz);
  652. xgmac_rx_refill(priv);
  653. priv->tx_tail = 0;
  654. priv->tx_head = 0;
  655. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  656. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  657. writel(priv->dma_rx_phy, priv->base + XGMAC_DMA_RX_BASE_ADDR);
  658. return 0;
  659. err_dma_tx:
  660. kfree(priv->tx_skbuff);
  661. err_tx_skb:
  662. dma_free_coherent(priv->device,
  663. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  664. priv->dma_rx, priv->dma_rx_phy);
  665. err_dma_rx:
  666. kfree(priv->rx_skbuff);
  667. return -ENOMEM;
  668. }
  669. static void xgmac_free_rx_skbufs(struct xgmac_priv *priv)
  670. {
  671. int i;
  672. struct xgmac_dma_desc *p;
  673. if (!priv->rx_skbuff)
  674. return;
  675. for (i = 0; i < DMA_RX_RING_SZ; i++) {
  676. if (priv->rx_skbuff[i] == NULL)
  677. continue;
  678. p = priv->dma_rx + i;
  679. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  680. priv->dma_buf_sz, DMA_FROM_DEVICE);
  681. dev_kfree_skb_any(priv->rx_skbuff[i]);
  682. priv->rx_skbuff[i] = NULL;
  683. }
  684. }
  685. static void xgmac_free_tx_skbufs(struct xgmac_priv *priv)
  686. {
  687. int i, f;
  688. struct xgmac_dma_desc *p;
  689. if (!priv->tx_skbuff)
  690. return;
  691. for (i = 0; i < DMA_TX_RING_SZ; i++) {
  692. if (priv->tx_skbuff[i] == NULL)
  693. continue;
  694. p = priv->dma_tx + i;
  695. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  696. desc_get_buf_len(p), DMA_TO_DEVICE);
  697. for (f = 0; f < skb_shinfo(priv->tx_skbuff[i])->nr_frags; f++) {
  698. p = priv->dma_tx + i++;
  699. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  700. desc_get_buf_len(p), DMA_TO_DEVICE);
  701. }
  702. dev_kfree_skb_any(priv->tx_skbuff[i]);
  703. priv->tx_skbuff[i] = NULL;
  704. }
  705. }
  706. static void xgmac_free_dma_desc_rings(struct xgmac_priv *priv)
  707. {
  708. /* Release the DMA TX/RX socket buffers */
  709. xgmac_free_rx_skbufs(priv);
  710. xgmac_free_tx_skbufs(priv);
  711. /* Free the consistent memory allocated for descriptor rings */
  712. if (priv->dma_tx) {
  713. dma_free_coherent(priv->device,
  714. DMA_TX_RING_SZ * sizeof(struct xgmac_dma_desc),
  715. priv->dma_tx, priv->dma_tx_phy);
  716. priv->dma_tx = NULL;
  717. }
  718. if (priv->dma_rx) {
  719. dma_free_coherent(priv->device,
  720. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  721. priv->dma_rx, priv->dma_rx_phy);
  722. priv->dma_rx = NULL;
  723. }
  724. kfree(priv->rx_skbuff);
  725. priv->rx_skbuff = NULL;
  726. kfree(priv->tx_skbuff);
  727. priv->tx_skbuff = NULL;
  728. }
  729. /**
  730. * xgmac_tx:
  731. * @priv: private driver structure
  732. * Description: it reclaims resources after transmission completes.
  733. */
  734. static void xgmac_tx_complete(struct xgmac_priv *priv)
  735. {
  736. int i;
  737. void __iomem *ioaddr = priv->base;
  738. writel(DMA_STATUS_TU | DMA_STATUS_NIS, ioaddr + XGMAC_DMA_STATUS);
  739. while (dma_ring_cnt(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ)) {
  740. unsigned int entry = priv->tx_tail;
  741. struct sk_buff *skb = priv->tx_skbuff[entry];
  742. struct xgmac_dma_desc *p = priv->dma_tx + entry;
  743. /* Check if the descriptor is owned by the DMA. */
  744. if (desc_get_owner(p))
  745. break;
  746. /* Verify tx error by looking at the last segment */
  747. if (desc_get_tx_ls(p))
  748. desc_get_tx_status(priv, p);
  749. netdev_dbg(priv->dev, "tx ring: curr %d, dirty %d\n",
  750. priv->tx_head, priv->tx_tail);
  751. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  752. desc_get_buf_len(p), DMA_TO_DEVICE);
  753. priv->tx_skbuff[entry] = NULL;
  754. priv->tx_tail = dma_ring_incr(entry, DMA_TX_RING_SZ);
  755. if (!skb) {
  756. continue;
  757. }
  758. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  759. entry = priv->tx_tail = dma_ring_incr(priv->tx_tail,
  760. DMA_TX_RING_SZ);
  761. p = priv->dma_tx + priv->tx_tail;
  762. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  763. desc_get_buf_len(p), DMA_TO_DEVICE);
  764. }
  765. /*
  766. * If there's room in the queue (limit it to size)
  767. * we add this skb back into the pool,
  768. * if it's the right size.
  769. */
  770. if ((skb_queue_len(&priv->rx_recycle) <
  771. DMA_RX_RING_SZ) &&
  772. skb_recycle_check(skb, priv->dma_buf_sz))
  773. __skb_queue_head(&priv->rx_recycle, skb);
  774. else
  775. dev_kfree_skb(skb);
  776. }
  777. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) >
  778. TX_THRESH)
  779. netif_wake_queue(priv->dev);
  780. }
  781. /**
  782. * xgmac_tx_err:
  783. * @priv: pointer to the private device structure
  784. * Description: it cleans the descriptors and restarts the transmission
  785. * in case of errors.
  786. */
  787. static void xgmac_tx_err(struct xgmac_priv *priv)
  788. {
  789. u32 reg, value, inten;
  790. netif_stop_queue(priv->dev);
  791. inten = readl(priv->base + XGMAC_DMA_INTR_ENA);
  792. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  793. reg = readl(priv->base + XGMAC_DMA_CONTROL);
  794. writel(reg & ~DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  795. do {
  796. value = readl(priv->base + XGMAC_DMA_STATUS) & 0x700000;
  797. } while (value && (value != 0x600000));
  798. xgmac_free_tx_skbufs(priv);
  799. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  800. priv->tx_tail = 0;
  801. priv->tx_head = 0;
  802. writel(reg | DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  803. writel(DMA_STATUS_TU | DMA_STATUS_TPS | DMA_STATUS_NIS | DMA_STATUS_AIS,
  804. priv->base + XGMAC_DMA_STATUS);
  805. writel(inten, priv->base + XGMAC_DMA_INTR_ENA);
  806. netif_wake_queue(priv->dev);
  807. }
  808. static int xgmac_hw_init(struct net_device *dev)
  809. {
  810. u32 value, ctrl;
  811. int limit;
  812. struct xgmac_priv *priv = netdev_priv(dev);
  813. void __iomem *ioaddr = priv->base;
  814. /* Save the ctrl register value */
  815. ctrl = readl(ioaddr + XGMAC_CONTROL) & XGMAC_CONTROL_SPD_MASK;
  816. /* SW reset */
  817. value = DMA_BUS_MODE_SFT_RESET;
  818. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  819. limit = 15000;
  820. while (limit-- &&
  821. (readl(ioaddr + XGMAC_DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  822. cpu_relax();
  823. if (limit < 0)
  824. return -EBUSY;
  825. value = (0x10 << DMA_BUS_MODE_PBL_SHIFT) |
  826. (0x10 << DMA_BUS_MODE_RPBL_SHIFT) |
  827. DMA_BUS_MODE_FB | DMA_BUS_MODE_ATDS | DMA_BUS_MODE_AAL;
  828. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  829. /* Enable interrupts */
  830. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  831. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  832. /* XGMAC requires AXI bus init. This is a 'magic number' for now */
  833. writel(0x000100E, ioaddr + XGMAC_DMA_AXI_BUS);
  834. ctrl |= XGMAC_CONTROL_DDIC | XGMAC_CONTROL_JE | XGMAC_CONTROL_ACS |
  835. XGMAC_CONTROL_CAR;
  836. if (dev->features & NETIF_F_RXCSUM)
  837. ctrl |= XGMAC_CONTROL_IPC;
  838. writel(ctrl, ioaddr + XGMAC_CONTROL);
  839. value = DMA_CONTROL_DFF;
  840. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  841. /* Set the HW DMA mode and the COE */
  842. writel(XGMAC_OMR_TSF | XGMAC_OMR_RSF | XGMAC_OMR_RFD | XGMAC_OMR_RFA,
  843. ioaddr + XGMAC_OMR);
  844. /* Reset the MMC counters */
  845. writel(1, ioaddr + XGMAC_MMC_CTRL);
  846. return 0;
  847. }
  848. /**
  849. * xgmac_open - open entry point of the driver
  850. * @dev : pointer to the device structure.
  851. * Description:
  852. * This function is the open entry point of the driver.
  853. * Return value:
  854. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  855. * file on failure.
  856. */
  857. static int xgmac_open(struct net_device *dev)
  858. {
  859. int ret;
  860. struct xgmac_priv *priv = netdev_priv(dev);
  861. void __iomem *ioaddr = priv->base;
  862. /* Check that the MAC address is valid. If its not, refuse
  863. * to bring the device up. The user must specify an
  864. * address using the following linux command:
  865. * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
  866. if (!is_valid_ether_addr(dev->dev_addr)) {
  867. eth_hw_addr_random(dev);
  868. netdev_dbg(priv->dev, "generated random MAC address %pM\n",
  869. dev->dev_addr);
  870. }
  871. skb_queue_head_init(&priv->rx_recycle);
  872. memset(&priv->xstats, 0, sizeof(struct xgmac_extra_stats));
  873. /* Initialize the XGMAC and descriptors */
  874. xgmac_hw_init(dev);
  875. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  876. xgmac_set_flow_ctrl(priv, priv->rx_pause, priv->tx_pause);
  877. ret = xgmac_dma_desc_rings_init(dev);
  878. if (ret < 0)
  879. return ret;
  880. /* Enable the MAC Rx/Tx */
  881. xgmac_mac_enable(ioaddr);
  882. napi_enable(&priv->napi);
  883. netif_start_queue(dev);
  884. return 0;
  885. }
  886. /**
  887. * xgmac_release - close entry point of the driver
  888. * @dev : device pointer.
  889. * Description:
  890. * This is the stop entry point of the driver.
  891. */
  892. static int xgmac_stop(struct net_device *dev)
  893. {
  894. struct xgmac_priv *priv = netdev_priv(dev);
  895. netif_stop_queue(dev);
  896. if (readl(priv->base + XGMAC_DMA_INTR_ENA))
  897. napi_disable(&priv->napi);
  898. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  899. skb_queue_purge(&priv->rx_recycle);
  900. /* Disable the MAC core */
  901. xgmac_mac_disable(priv->base);
  902. /* Release and free the Rx/Tx resources */
  903. xgmac_free_dma_desc_rings(priv);
  904. return 0;
  905. }
  906. /**
  907. * xgmac_xmit:
  908. * @skb : the socket buffer
  909. * @dev : device pointer
  910. * Description : Tx entry point of the driver.
  911. */
  912. static netdev_tx_t xgmac_xmit(struct sk_buff *skb, struct net_device *dev)
  913. {
  914. struct xgmac_priv *priv = netdev_priv(dev);
  915. unsigned int entry;
  916. int i;
  917. int nfrags = skb_shinfo(skb)->nr_frags;
  918. struct xgmac_dma_desc *desc, *first;
  919. unsigned int desc_flags;
  920. unsigned int len;
  921. dma_addr_t paddr;
  922. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) <
  923. (nfrags + 1)) {
  924. writel(DMA_INTR_DEFAULT_MASK | DMA_INTR_ENA_TIE,
  925. priv->base + XGMAC_DMA_INTR_ENA);
  926. netif_stop_queue(dev);
  927. return NETDEV_TX_BUSY;
  928. }
  929. desc_flags = (skb->ip_summed == CHECKSUM_PARTIAL) ?
  930. TXDESC_CSUM_ALL : 0;
  931. entry = priv->tx_head;
  932. desc = priv->dma_tx + entry;
  933. first = desc;
  934. len = skb_headlen(skb);
  935. paddr = dma_map_single(priv->device, skb->data, len, DMA_TO_DEVICE);
  936. if (dma_mapping_error(priv->device, paddr)) {
  937. dev_kfree_skb(skb);
  938. return -EIO;
  939. }
  940. priv->tx_skbuff[entry] = skb;
  941. desc_set_buf_addr_and_size(desc, paddr, len);
  942. for (i = 0; i < nfrags; i++) {
  943. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  944. len = frag->size;
  945. paddr = skb_frag_dma_map(priv->device, frag, 0, len,
  946. DMA_TO_DEVICE);
  947. if (dma_mapping_error(priv->device, paddr)) {
  948. dev_kfree_skb(skb);
  949. return -EIO;
  950. }
  951. entry = dma_ring_incr(entry, DMA_TX_RING_SZ);
  952. desc = priv->dma_tx + entry;
  953. priv->tx_skbuff[entry] = NULL;
  954. desc_set_buf_addr_and_size(desc, paddr, len);
  955. if (i < (nfrags - 1))
  956. desc_set_tx_owner(desc, desc_flags);
  957. }
  958. /* Interrupt on completition only for the latest segment */
  959. if (desc != first)
  960. desc_set_tx_owner(desc, desc_flags |
  961. TXDESC_LAST_SEG | TXDESC_INTERRUPT);
  962. else
  963. desc_flags |= TXDESC_LAST_SEG | TXDESC_INTERRUPT;
  964. /* Set owner on first desc last to avoid race condition */
  965. wmb();
  966. desc_set_tx_owner(first, desc_flags | TXDESC_FIRST_SEG);
  967. priv->tx_head = dma_ring_incr(entry, DMA_TX_RING_SZ);
  968. writel(1, priv->base + XGMAC_DMA_TX_POLL);
  969. return NETDEV_TX_OK;
  970. }
  971. static int xgmac_rx(struct xgmac_priv *priv, int limit)
  972. {
  973. unsigned int entry;
  974. unsigned int count = 0;
  975. struct xgmac_dma_desc *p;
  976. while (count < limit) {
  977. int ip_checksum;
  978. struct sk_buff *skb;
  979. int frame_len;
  980. writel(DMA_STATUS_RI | DMA_STATUS_NIS,
  981. priv->base + XGMAC_DMA_STATUS);
  982. entry = priv->rx_tail;
  983. p = priv->dma_rx + entry;
  984. if (desc_get_owner(p))
  985. break;
  986. count++;
  987. priv->rx_tail = dma_ring_incr(priv->rx_tail, DMA_RX_RING_SZ);
  988. /* read the status of the incoming frame */
  989. ip_checksum = desc_get_rx_status(priv, p);
  990. if (ip_checksum < 0)
  991. continue;
  992. skb = priv->rx_skbuff[entry];
  993. if (unlikely(!skb)) {
  994. netdev_err(priv->dev, "Inconsistent Rx descriptor chain\n");
  995. break;
  996. }
  997. priv->rx_skbuff[entry] = NULL;
  998. frame_len = desc_get_rx_frame_len(p);
  999. netdev_dbg(priv->dev, "RX frame size %d, COE status: %d\n",
  1000. frame_len, ip_checksum);
  1001. skb_put(skb, frame_len);
  1002. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  1003. frame_len, DMA_FROM_DEVICE);
  1004. skb->protocol = eth_type_trans(skb, priv->dev);
  1005. skb->ip_summed = ip_checksum;
  1006. if (ip_checksum == CHECKSUM_NONE)
  1007. netif_receive_skb(skb);
  1008. else
  1009. napi_gro_receive(&priv->napi, skb);
  1010. }
  1011. xgmac_rx_refill(priv);
  1012. writel(1, priv->base + XGMAC_DMA_RX_POLL);
  1013. return count;
  1014. }
  1015. /**
  1016. * xgmac_poll - xgmac poll method (NAPI)
  1017. * @napi : pointer to the napi structure.
  1018. * @budget : maximum number of packets that the current CPU can receive from
  1019. * all interfaces.
  1020. * Description :
  1021. * This function implements the the reception process.
  1022. * Also it runs the TX completion thread
  1023. */
  1024. static int xgmac_poll(struct napi_struct *napi, int budget)
  1025. {
  1026. struct xgmac_priv *priv = container_of(napi,
  1027. struct xgmac_priv, napi);
  1028. int work_done = 0;
  1029. xgmac_tx_complete(priv);
  1030. work_done = xgmac_rx(priv, budget);
  1031. if (work_done < budget) {
  1032. napi_complete(napi);
  1033. writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_INTR_ENA);
  1034. }
  1035. return work_done;
  1036. }
  1037. /**
  1038. * xgmac_tx_timeout
  1039. * @dev : Pointer to net device structure
  1040. * Description: this function is called when a packet transmission fails to
  1041. * complete within a reasonable tmrate. The driver will mark the error in the
  1042. * netdev structure and arrange for the device to be reset to a sane state
  1043. * in order to transmit a new packet.
  1044. */
  1045. static void xgmac_tx_timeout(struct net_device *dev)
  1046. {
  1047. struct xgmac_priv *priv = netdev_priv(dev);
  1048. /* Clear Tx resources and restart transmitting again */
  1049. xgmac_tx_err(priv);
  1050. }
  1051. /**
  1052. * xgmac_set_rx_mode - entry point for multicast addressing
  1053. * @dev : pointer to the device structure
  1054. * Description:
  1055. * This function is a driver entry point which gets called by the kernel
  1056. * whenever multicast addresses must be enabled/disabled.
  1057. * Return value:
  1058. * void.
  1059. */
  1060. static void xgmac_set_rx_mode(struct net_device *dev)
  1061. {
  1062. int i;
  1063. struct xgmac_priv *priv = netdev_priv(dev);
  1064. void __iomem *ioaddr = priv->base;
  1065. unsigned int value = 0;
  1066. u32 hash_filter[XGMAC_NUM_HASH];
  1067. int reg = 1;
  1068. struct netdev_hw_addr *ha;
  1069. bool use_hash = false;
  1070. netdev_dbg(priv->dev, "# mcasts %d, # unicast %d\n",
  1071. netdev_mc_count(dev), netdev_uc_count(dev));
  1072. if (dev->flags & IFF_PROMISC) {
  1073. writel(XGMAC_FRAME_FILTER_PR, ioaddr + XGMAC_FRAME_FILTER);
  1074. return;
  1075. }
  1076. memset(hash_filter, 0, sizeof(hash_filter));
  1077. if (netdev_uc_count(dev) > XGMAC_MAX_FILTER_ADDR) {
  1078. use_hash = true;
  1079. value |= XGMAC_FRAME_FILTER_HUC | XGMAC_FRAME_FILTER_HPF;
  1080. }
  1081. netdev_for_each_uc_addr(ha, dev) {
  1082. if (use_hash) {
  1083. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1084. /* The most significant 4 bits determine the register to
  1085. * use (H/L) while the other 5 bits determine the bit
  1086. * within the register. */
  1087. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1088. } else {
  1089. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1090. reg++;
  1091. }
  1092. }
  1093. if (dev->flags & IFF_ALLMULTI) {
  1094. value |= XGMAC_FRAME_FILTER_PM;
  1095. goto out;
  1096. }
  1097. if ((netdev_mc_count(dev) + reg - 1) > XGMAC_MAX_FILTER_ADDR) {
  1098. use_hash = true;
  1099. value |= XGMAC_FRAME_FILTER_HMC | XGMAC_FRAME_FILTER_HPF;
  1100. }
  1101. netdev_for_each_mc_addr(ha, dev) {
  1102. if (use_hash) {
  1103. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1104. /* The most significant 4 bits determine the register to
  1105. * use (H/L) while the other 5 bits determine the bit
  1106. * within the register. */
  1107. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1108. } else {
  1109. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1110. reg++;
  1111. }
  1112. }
  1113. out:
  1114. for (i = 0; i < XGMAC_NUM_HASH; i++)
  1115. writel(hash_filter[i], ioaddr + XGMAC_HASH(i));
  1116. writel(value, ioaddr + XGMAC_FRAME_FILTER);
  1117. }
  1118. /**
  1119. * xgmac_change_mtu - entry point to change MTU size for the device.
  1120. * @dev : device pointer.
  1121. * @new_mtu : the new MTU size for the device.
  1122. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1123. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1124. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1125. * Return value:
  1126. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1127. * file on failure.
  1128. */
  1129. static int xgmac_change_mtu(struct net_device *dev, int new_mtu)
  1130. {
  1131. struct xgmac_priv *priv = netdev_priv(dev);
  1132. int old_mtu;
  1133. if ((new_mtu < 46) || (new_mtu > MAX_MTU)) {
  1134. netdev_err(priv->dev, "invalid MTU, max MTU is: %d\n", MAX_MTU);
  1135. return -EINVAL;
  1136. }
  1137. old_mtu = dev->mtu;
  1138. dev->mtu = new_mtu;
  1139. /* return early if the buffer sizes will not change */
  1140. if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
  1141. return 0;
  1142. if (old_mtu == new_mtu)
  1143. return 0;
  1144. /* Stop everything, get ready to change the MTU */
  1145. if (!netif_running(dev))
  1146. return 0;
  1147. /* Bring the interface down and then back up */
  1148. xgmac_stop(dev);
  1149. return xgmac_open(dev);
  1150. }
  1151. static irqreturn_t xgmac_pmt_interrupt(int irq, void *dev_id)
  1152. {
  1153. u32 intr_status;
  1154. struct net_device *dev = (struct net_device *)dev_id;
  1155. struct xgmac_priv *priv = netdev_priv(dev);
  1156. void __iomem *ioaddr = priv->base;
  1157. intr_status = readl(ioaddr + XGMAC_INT_STAT);
  1158. if (intr_status & XGMAC_INT_STAT_PMT) {
  1159. netdev_dbg(priv->dev, "received Magic frame\n");
  1160. /* clear the PMT bits 5 and 6 by reading the PMT */
  1161. readl(ioaddr + XGMAC_PMT);
  1162. }
  1163. return IRQ_HANDLED;
  1164. }
  1165. static irqreturn_t xgmac_interrupt(int irq, void *dev_id)
  1166. {
  1167. u32 intr_status;
  1168. bool tx_err = false;
  1169. struct net_device *dev = (struct net_device *)dev_id;
  1170. struct xgmac_priv *priv = netdev_priv(dev);
  1171. struct xgmac_extra_stats *x = &priv->xstats;
  1172. /* read the status register (CSR5) */
  1173. intr_status = readl(priv->base + XGMAC_DMA_STATUS);
  1174. intr_status &= readl(priv->base + XGMAC_DMA_INTR_ENA);
  1175. writel(intr_status, priv->base + XGMAC_DMA_STATUS);
  1176. /* It displays the DMA process states (CSR5 register) */
  1177. /* ABNORMAL interrupts */
  1178. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  1179. if (intr_status & DMA_STATUS_TJT) {
  1180. netdev_err(priv->dev, "transmit jabber\n");
  1181. x->tx_jabber++;
  1182. }
  1183. if (intr_status & DMA_STATUS_RU)
  1184. x->rx_buf_unav++;
  1185. if (intr_status & DMA_STATUS_RPS) {
  1186. netdev_err(priv->dev, "receive process stopped\n");
  1187. x->rx_process_stopped++;
  1188. }
  1189. if (intr_status & DMA_STATUS_ETI) {
  1190. netdev_err(priv->dev, "transmit early interrupt\n");
  1191. x->tx_early++;
  1192. }
  1193. if (intr_status & DMA_STATUS_TPS) {
  1194. netdev_err(priv->dev, "transmit process stopped\n");
  1195. x->tx_process_stopped++;
  1196. tx_err = true;
  1197. }
  1198. if (intr_status & DMA_STATUS_FBI) {
  1199. netdev_err(priv->dev, "fatal bus error\n");
  1200. x->fatal_bus_error++;
  1201. tx_err = true;
  1202. }
  1203. if (tx_err)
  1204. xgmac_tx_err(priv);
  1205. }
  1206. /* TX/RX NORMAL interrupts */
  1207. if (intr_status & (DMA_STATUS_RI | DMA_STATUS_TU)) {
  1208. writel(DMA_INTR_ABNORMAL, priv->base + XGMAC_DMA_INTR_ENA);
  1209. napi_schedule(&priv->napi);
  1210. }
  1211. return IRQ_HANDLED;
  1212. }
  1213. #ifdef CONFIG_NET_POLL_CONTROLLER
  1214. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1215. * to allow network I/O with interrupts disabled. */
  1216. static void xgmac_poll_controller(struct net_device *dev)
  1217. {
  1218. disable_irq(dev->irq);
  1219. xgmac_interrupt(dev->irq, dev);
  1220. enable_irq(dev->irq);
  1221. }
  1222. #endif
  1223. static struct rtnl_link_stats64 *
  1224. xgmac_get_stats64(struct net_device *dev,
  1225. struct rtnl_link_stats64 *storage)
  1226. {
  1227. struct xgmac_priv *priv = netdev_priv(dev);
  1228. void __iomem *base = priv->base;
  1229. u32 count;
  1230. spin_lock_bh(&priv->stats_lock);
  1231. writel(XGMAC_MMC_CTRL_CNT_FRZ, base + XGMAC_MMC_CTRL);
  1232. storage->rx_bytes = readl(base + XGMAC_MMC_RXOCTET_G_LO);
  1233. storage->rx_bytes |= (u64)(readl(base + XGMAC_MMC_RXOCTET_G_HI)) << 32;
  1234. storage->rx_packets = readl(base + XGMAC_MMC_RXFRAME_GB_LO);
  1235. storage->multicast = readl(base + XGMAC_MMC_RXMCFRAME_G);
  1236. storage->rx_crc_errors = readl(base + XGMAC_MMC_RXCRCERR);
  1237. storage->rx_length_errors = readl(base + XGMAC_MMC_RXLENGTHERR);
  1238. storage->rx_missed_errors = readl(base + XGMAC_MMC_RXOVERFLOW);
  1239. storage->tx_bytes = readl(base + XGMAC_MMC_TXOCTET_G_LO);
  1240. storage->tx_bytes |= (u64)(readl(base + XGMAC_MMC_TXOCTET_G_HI)) << 32;
  1241. count = readl(base + XGMAC_MMC_TXFRAME_GB_LO);
  1242. storage->tx_errors = count - readl(base + XGMAC_MMC_TXFRAME_G_LO);
  1243. storage->tx_packets = count;
  1244. storage->tx_fifo_errors = readl(base + XGMAC_MMC_TXUNDERFLOW);
  1245. writel(0, base + XGMAC_MMC_CTRL);
  1246. spin_unlock_bh(&priv->stats_lock);
  1247. return storage;
  1248. }
  1249. static int xgmac_set_mac_address(struct net_device *dev, void *p)
  1250. {
  1251. struct xgmac_priv *priv = netdev_priv(dev);
  1252. void __iomem *ioaddr = priv->base;
  1253. struct sockaddr *addr = p;
  1254. if (!is_valid_ether_addr(addr->sa_data))
  1255. return -EADDRNOTAVAIL;
  1256. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  1257. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1258. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  1259. return 0;
  1260. }
  1261. static int xgmac_set_features(struct net_device *dev, netdev_features_t features)
  1262. {
  1263. u32 ctrl;
  1264. struct xgmac_priv *priv = netdev_priv(dev);
  1265. void __iomem *ioaddr = priv->base;
  1266. u32 changed = dev->features ^ features;
  1267. if (!(changed & NETIF_F_RXCSUM))
  1268. return 0;
  1269. ctrl = readl(ioaddr + XGMAC_CONTROL);
  1270. if (features & NETIF_F_RXCSUM)
  1271. ctrl |= XGMAC_CONTROL_IPC;
  1272. else
  1273. ctrl &= ~XGMAC_CONTROL_IPC;
  1274. writel(ctrl, ioaddr + XGMAC_CONTROL);
  1275. return 0;
  1276. }
  1277. static const struct net_device_ops xgmac_netdev_ops = {
  1278. .ndo_open = xgmac_open,
  1279. .ndo_start_xmit = xgmac_xmit,
  1280. .ndo_stop = xgmac_stop,
  1281. .ndo_change_mtu = xgmac_change_mtu,
  1282. .ndo_set_rx_mode = xgmac_set_rx_mode,
  1283. .ndo_tx_timeout = xgmac_tx_timeout,
  1284. .ndo_get_stats64 = xgmac_get_stats64,
  1285. #ifdef CONFIG_NET_POLL_CONTROLLER
  1286. .ndo_poll_controller = xgmac_poll_controller,
  1287. #endif
  1288. .ndo_set_mac_address = xgmac_set_mac_address,
  1289. .ndo_set_features = xgmac_set_features,
  1290. };
  1291. static int xgmac_ethtool_getsettings(struct net_device *dev,
  1292. struct ethtool_cmd *cmd)
  1293. {
  1294. cmd->autoneg = 0;
  1295. cmd->duplex = DUPLEX_FULL;
  1296. ethtool_cmd_speed_set(cmd, 10000);
  1297. cmd->supported = 0;
  1298. cmd->advertising = 0;
  1299. cmd->transceiver = XCVR_INTERNAL;
  1300. return 0;
  1301. }
  1302. static void xgmac_get_pauseparam(struct net_device *netdev,
  1303. struct ethtool_pauseparam *pause)
  1304. {
  1305. struct xgmac_priv *priv = netdev_priv(netdev);
  1306. pause->rx_pause = priv->rx_pause;
  1307. pause->tx_pause = priv->tx_pause;
  1308. }
  1309. static int xgmac_set_pauseparam(struct net_device *netdev,
  1310. struct ethtool_pauseparam *pause)
  1311. {
  1312. struct xgmac_priv *priv = netdev_priv(netdev);
  1313. if (pause->autoneg)
  1314. return -EINVAL;
  1315. return xgmac_set_flow_ctrl(priv, pause->rx_pause, pause->tx_pause);
  1316. }
  1317. struct xgmac_stats {
  1318. char stat_string[ETH_GSTRING_LEN];
  1319. int stat_offset;
  1320. bool is_reg;
  1321. };
  1322. #define XGMAC_STAT(m) \
  1323. { #m, offsetof(struct xgmac_priv, xstats.m), false }
  1324. #define XGMAC_HW_STAT(m, reg_offset) \
  1325. { #m, reg_offset, true }
  1326. static const struct xgmac_stats xgmac_gstrings_stats[] = {
  1327. XGMAC_STAT(tx_frame_flushed),
  1328. XGMAC_STAT(tx_payload_error),
  1329. XGMAC_STAT(tx_ip_header_error),
  1330. XGMAC_STAT(tx_local_fault),
  1331. XGMAC_STAT(tx_remote_fault),
  1332. XGMAC_STAT(tx_early),
  1333. XGMAC_STAT(tx_process_stopped),
  1334. XGMAC_STAT(tx_jabber),
  1335. XGMAC_STAT(rx_buf_unav),
  1336. XGMAC_STAT(rx_process_stopped),
  1337. XGMAC_STAT(rx_payload_error),
  1338. XGMAC_STAT(rx_ip_header_error),
  1339. XGMAC_STAT(rx_da_filter_fail),
  1340. XGMAC_STAT(rx_sa_filter_fail),
  1341. XGMAC_STAT(fatal_bus_error),
  1342. XGMAC_HW_STAT(rx_watchdog, XGMAC_MMC_RXWATCHDOG),
  1343. XGMAC_HW_STAT(tx_vlan, XGMAC_MMC_TXVLANFRAME),
  1344. XGMAC_HW_STAT(rx_vlan, XGMAC_MMC_RXVLANFRAME),
  1345. XGMAC_HW_STAT(tx_pause, XGMAC_MMC_TXPAUSEFRAME),
  1346. XGMAC_HW_STAT(rx_pause, XGMAC_MMC_RXPAUSEFRAME),
  1347. };
  1348. #define XGMAC_STATS_LEN ARRAY_SIZE(xgmac_gstrings_stats)
  1349. static void xgmac_get_ethtool_stats(struct net_device *dev,
  1350. struct ethtool_stats *dummy,
  1351. u64 *data)
  1352. {
  1353. struct xgmac_priv *priv = netdev_priv(dev);
  1354. void *p = priv;
  1355. int i;
  1356. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1357. if (xgmac_gstrings_stats[i].is_reg)
  1358. *data++ = readl(priv->base +
  1359. xgmac_gstrings_stats[i].stat_offset);
  1360. else
  1361. *data++ = *(u32 *)(p +
  1362. xgmac_gstrings_stats[i].stat_offset);
  1363. }
  1364. }
  1365. static int xgmac_get_sset_count(struct net_device *netdev, int sset)
  1366. {
  1367. switch (sset) {
  1368. case ETH_SS_STATS:
  1369. return XGMAC_STATS_LEN;
  1370. default:
  1371. return -EINVAL;
  1372. }
  1373. }
  1374. static void xgmac_get_strings(struct net_device *dev, u32 stringset,
  1375. u8 *data)
  1376. {
  1377. int i;
  1378. u8 *p = data;
  1379. switch (stringset) {
  1380. case ETH_SS_STATS:
  1381. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1382. memcpy(p, xgmac_gstrings_stats[i].stat_string,
  1383. ETH_GSTRING_LEN);
  1384. p += ETH_GSTRING_LEN;
  1385. }
  1386. break;
  1387. default:
  1388. WARN_ON(1);
  1389. break;
  1390. }
  1391. }
  1392. static void xgmac_get_wol(struct net_device *dev,
  1393. struct ethtool_wolinfo *wol)
  1394. {
  1395. struct xgmac_priv *priv = netdev_priv(dev);
  1396. if (device_can_wakeup(priv->device)) {
  1397. wol->supported = WAKE_MAGIC | WAKE_UCAST;
  1398. wol->wolopts = priv->wolopts;
  1399. }
  1400. }
  1401. static int xgmac_set_wol(struct net_device *dev,
  1402. struct ethtool_wolinfo *wol)
  1403. {
  1404. struct xgmac_priv *priv = netdev_priv(dev);
  1405. u32 support = WAKE_MAGIC | WAKE_UCAST;
  1406. if (!device_can_wakeup(priv->device))
  1407. return -ENOTSUPP;
  1408. if (wol->wolopts & ~support)
  1409. return -EINVAL;
  1410. priv->wolopts = wol->wolopts;
  1411. if (wol->wolopts) {
  1412. device_set_wakeup_enable(priv->device, 1);
  1413. enable_irq_wake(dev->irq);
  1414. } else {
  1415. device_set_wakeup_enable(priv->device, 0);
  1416. disable_irq_wake(dev->irq);
  1417. }
  1418. return 0;
  1419. }
  1420. static const struct ethtool_ops xgmac_ethtool_ops = {
  1421. .get_settings = xgmac_ethtool_getsettings,
  1422. .get_link = ethtool_op_get_link,
  1423. .get_pauseparam = xgmac_get_pauseparam,
  1424. .set_pauseparam = xgmac_set_pauseparam,
  1425. .get_ethtool_stats = xgmac_get_ethtool_stats,
  1426. .get_strings = xgmac_get_strings,
  1427. .get_wol = xgmac_get_wol,
  1428. .set_wol = xgmac_set_wol,
  1429. .get_sset_count = xgmac_get_sset_count,
  1430. };
  1431. /**
  1432. * xgmac_probe
  1433. * @pdev: platform device pointer
  1434. * Description: the driver is initialized through platform_device.
  1435. */
  1436. static int xgmac_probe(struct platform_device *pdev)
  1437. {
  1438. int ret = 0;
  1439. struct resource *res;
  1440. struct net_device *ndev = NULL;
  1441. struct xgmac_priv *priv = NULL;
  1442. u32 uid;
  1443. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1444. if (!res)
  1445. return -ENODEV;
  1446. if (!request_mem_region(res->start, resource_size(res), pdev->name))
  1447. return -EBUSY;
  1448. ndev = alloc_etherdev(sizeof(struct xgmac_priv));
  1449. if (!ndev) {
  1450. ret = -ENOMEM;
  1451. goto err_alloc;
  1452. }
  1453. SET_NETDEV_DEV(ndev, &pdev->dev);
  1454. priv = netdev_priv(ndev);
  1455. platform_set_drvdata(pdev, ndev);
  1456. ether_setup(ndev);
  1457. ndev->netdev_ops = &xgmac_netdev_ops;
  1458. SET_ETHTOOL_OPS(ndev, &xgmac_ethtool_ops);
  1459. spin_lock_init(&priv->stats_lock);
  1460. priv->device = &pdev->dev;
  1461. priv->dev = ndev;
  1462. priv->rx_pause = 1;
  1463. priv->tx_pause = 1;
  1464. priv->base = ioremap(res->start, resource_size(res));
  1465. if (!priv->base) {
  1466. netdev_err(ndev, "ioremap failed\n");
  1467. ret = -ENOMEM;
  1468. goto err_io;
  1469. }
  1470. uid = readl(priv->base + XGMAC_VERSION);
  1471. netdev_info(ndev, "h/w version is 0x%x\n", uid);
  1472. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1473. ndev->irq = platform_get_irq(pdev, 0);
  1474. if (ndev->irq == -ENXIO) {
  1475. netdev_err(ndev, "No irq resource\n");
  1476. ret = ndev->irq;
  1477. goto err_irq;
  1478. }
  1479. ret = request_irq(ndev->irq, xgmac_interrupt, 0,
  1480. dev_name(&pdev->dev), ndev);
  1481. if (ret < 0) {
  1482. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1483. ndev->irq, ret);
  1484. goto err_irq;
  1485. }
  1486. priv->pmt_irq = platform_get_irq(pdev, 1);
  1487. if (priv->pmt_irq == -ENXIO) {
  1488. netdev_err(ndev, "No pmt irq resource\n");
  1489. ret = priv->pmt_irq;
  1490. goto err_pmt_irq;
  1491. }
  1492. ret = request_irq(priv->pmt_irq, xgmac_pmt_interrupt, 0,
  1493. dev_name(&pdev->dev), ndev);
  1494. if (ret < 0) {
  1495. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1496. priv->pmt_irq, ret);
  1497. goto err_pmt_irq;
  1498. }
  1499. device_set_wakeup_capable(&pdev->dev, 1);
  1500. if (device_can_wakeup(priv->device))
  1501. priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
  1502. ndev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
  1503. if (readl(priv->base + XGMAC_DMA_HW_FEATURE) & DMA_HW_FEAT_TXCOESEL)
  1504. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1505. NETIF_F_RXCSUM;
  1506. ndev->features |= ndev->hw_features;
  1507. ndev->priv_flags |= IFF_UNICAST_FLT;
  1508. /* Get the MAC address */
  1509. xgmac_get_mac_addr(priv->base, ndev->dev_addr, 0);
  1510. if (!is_valid_ether_addr(ndev->dev_addr))
  1511. netdev_warn(ndev, "MAC address %pM not valid",
  1512. ndev->dev_addr);
  1513. netif_napi_add(ndev, &priv->napi, xgmac_poll, 64);
  1514. ret = register_netdev(ndev);
  1515. if (ret)
  1516. goto err_reg;
  1517. return 0;
  1518. err_reg:
  1519. netif_napi_del(&priv->napi);
  1520. free_irq(priv->pmt_irq, ndev);
  1521. err_pmt_irq:
  1522. free_irq(ndev->irq, ndev);
  1523. err_irq:
  1524. iounmap(priv->base);
  1525. err_io:
  1526. free_netdev(ndev);
  1527. err_alloc:
  1528. release_mem_region(res->start, resource_size(res));
  1529. platform_set_drvdata(pdev, NULL);
  1530. return ret;
  1531. }
  1532. /**
  1533. * xgmac_dvr_remove
  1534. * @pdev: platform device pointer
  1535. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1536. * changes the link status, releases the DMA descriptor rings,
  1537. * unregisters the MDIO bus and unmaps the allocated memory.
  1538. */
  1539. static int xgmac_remove(struct platform_device *pdev)
  1540. {
  1541. struct net_device *ndev = platform_get_drvdata(pdev);
  1542. struct xgmac_priv *priv = netdev_priv(ndev);
  1543. struct resource *res;
  1544. xgmac_mac_disable(priv->base);
  1545. /* Free the IRQ lines */
  1546. free_irq(ndev->irq, ndev);
  1547. free_irq(priv->pmt_irq, ndev);
  1548. platform_set_drvdata(pdev, NULL);
  1549. unregister_netdev(ndev);
  1550. netif_napi_del(&priv->napi);
  1551. iounmap(priv->base);
  1552. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1553. release_mem_region(res->start, resource_size(res));
  1554. free_netdev(ndev);
  1555. return 0;
  1556. }
  1557. #ifdef CONFIG_PM_SLEEP
  1558. static void xgmac_pmt(void __iomem *ioaddr, unsigned long mode)
  1559. {
  1560. unsigned int pmt = 0;
  1561. if (mode & WAKE_MAGIC)
  1562. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_MAGIC_PKT;
  1563. if (mode & WAKE_UCAST)
  1564. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_GLBL_UNICAST;
  1565. writel(pmt, ioaddr + XGMAC_PMT);
  1566. }
  1567. static int xgmac_suspend(struct device *dev)
  1568. {
  1569. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1570. struct xgmac_priv *priv = netdev_priv(ndev);
  1571. u32 value;
  1572. if (!ndev || !netif_running(ndev))
  1573. return 0;
  1574. netif_device_detach(ndev);
  1575. napi_disable(&priv->napi);
  1576. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1577. if (device_may_wakeup(priv->device)) {
  1578. /* Stop TX/RX DMA Only */
  1579. value = readl(priv->base + XGMAC_DMA_CONTROL);
  1580. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  1581. writel(value, priv->base + XGMAC_DMA_CONTROL);
  1582. xgmac_pmt(priv->base, priv->wolopts);
  1583. } else
  1584. xgmac_mac_disable(priv->base);
  1585. return 0;
  1586. }
  1587. static int xgmac_resume(struct device *dev)
  1588. {
  1589. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1590. struct xgmac_priv *priv = netdev_priv(ndev);
  1591. void __iomem *ioaddr = priv->base;
  1592. if (!netif_running(ndev))
  1593. return 0;
  1594. xgmac_pmt(ioaddr, 0);
  1595. /* Enable the MAC and DMA */
  1596. xgmac_mac_enable(ioaddr);
  1597. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  1598. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  1599. netif_device_attach(ndev);
  1600. napi_enable(&priv->napi);
  1601. return 0;
  1602. }
  1603. static SIMPLE_DEV_PM_OPS(xgmac_pm_ops, xgmac_suspend, xgmac_resume);
  1604. #define XGMAC_PM_OPS (&xgmac_pm_ops)
  1605. #else
  1606. #define XGMAC_PM_OPS NULL
  1607. #endif /* CONFIG_PM_SLEEP */
  1608. static const struct of_device_id xgmac_of_match[] = {
  1609. { .compatible = "calxeda,hb-xgmac", },
  1610. {},
  1611. };
  1612. MODULE_DEVICE_TABLE(of, xgmac_of_match);
  1613. static struct platform_driver xgmac_driver = {
  1614. .driver = {
  1615. .name = "calxedaxgmac",
  1616. .of_match_table = xgmac_of_match,
  1617. },
  1618. .probe = xgmac_probe,
  1619. .remove = xgmac_remove,
  1620. .driver.pm = XGMAC_PM_OPS,
  1621. };
  1622. module_platform_driver(xgmac_driver);
  1623. MODULE_AUTHOR("Calxeda, Inc.");
  1624. MODULE_DESCRIPTION("Calxeda 10G XGMAC driver");
  1625. MODULE_LICENSE("GPL v2");