xgmac.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  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. /* Check if packet has checksum already */
  487. if ((status & RXDESC_FRAME_TYPE) && (status & RXDESC_EXT_STATUS) &&
  488. !(ext_status & RXDESC_IP_PAYLOAD_MASK))
  489. ret = CHECKSUM_NONE;
  490. netdev_dbg(priv->dev, "rx status - frame type=%d, csum = %d, ext stat %08x\n",
  491. (status & RXDESC_FRAME_TYPE) ? 1 : 0, ret, ext_status);
  492. if (!(status & RXDESC_ERROR_SUMMARY))
  493. return ret;
  494. /* Handle any errors */
  495. if (status & (RXDESC_DESCRIPTOR_ERR | RXDESC_OVERFLOW_ERR |
  496. RXDESC_GIANT_FRAME | RXDESC_LENGTH_ERR | RXDESC_CRC_ERR))
  497. return -1;
  498. if (status & RXDESC_EXT_STATUS) {
  499. if (ext_status & RXDESC_IP_HEADER_ERR)
  500. x->rx_ip_header_error++;
  501. if (ext_status & RXDESC_IP_PAYLOAD_ERR)
  502. x->rx_payload_error++;
  503. netdev_dbg(priv->dev, "IP checksum error - stat %08x\n",
  504. ext_status);
  505. return CHECKSUM_NONE;
  506. }
  507. return ret;
  508. }
  509. static inline void xgmac_mac_enable(void __iomem *ioaddr)
  510. {
  511. u32 value = readl(ioaddr + XGMAC_CONTROL);
  512. value |= MAC_ENABLE_RX | MAC_ENABLE_TX;
  513. writel(value, ioaddr + XGMAC_CONTROL);
  514. value = readl(ioaddr + XGMAC_DMA_CONTROL);
  515. value |= DMA_CONTROL_ST | DMA_CONTROL_SR;
  516. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  517. }
  518. static inline void xgmac_mac_disable(void __iomem *ioaddr)
  519. {
  520. u32 value = readl(ioaddr + XGMAC_DMA_CONTROL);
  521. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  522. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  523. value = readl(ioaddr + XGMAC_CONTROL);
  524. value &= ~(MAC_ENABLE_TX | MAC_ENABLE_RX);
  525. writel(value, ioaddr + XGMAC_CONTROL);
  526. }
  527. static void xgmac_set_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  528. int num)
  529. {
  530. u32 data;
  531. data = (addr[5] << 8) | addr[4] | (num ? XGMAC_ADDR_AE : 0);
  532. writel(data, ioaddr + XGMAC_ADDR_HIGH(num));
  533. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  534. writel(data, ioaddr + XGMAC_ADDR_LOW(num));
  535. }
  536. static void xgmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  537. int num)
  538. {
  539. u32 hi_addr, lo_addr;
  540. /* Read the MAC address from the hardware */
  541. hi_addr = readl(ioaddr + XGMAC_ADDR_HIGH(num));
  542. lo_addr = readl(ioaddr + XGMAC_ADDR_LOW(num));
  543. /* Extract the MAC address from the high and low words */
  544. addr[0] = lo_addr & 0xff;
  545. addr[1] = (lo_addr >> 8) & 0xff;
  546. addr[2] = (lo_addr >> 16) & 0xff;
  547. addr[3] = (lo_addr >> 24) & 0xff;
  548. addr[4] = hi_addr & 0xff;
  549. addr[5] = (hi_addr >> 8) & 0xff;
  550. }
  551. static int xgmac_set_flow_ctrl(struct xgmac_priv *priv, int rx, int tx)
  552. {
  553. u32 reg;
  554. unsigned int flow = 0;
  555. priv->rx_pause = rx;
  556. priv->tx_pause = tx;
  557. if (rx || tx) {
  558. if (rx)
  559. flow |= XGMAC_FLOW_CTRL_RFE;
  560. if (tx)
  561. flow |= XGMAC_FLOW_CTRL_TFE;
  562. flow |= XGMAC_FLOW_CTRL_PLT | XGMAC_FLOW_CTRL_UP;
  563. flow |= (PAUSE_TIME << XGMAC_FLOW_CTRL_PT_SHIFT);
  564. writel(flow, priv->base + XGMAC_FLOW_CTRL);
  565. reg = readl(priv->base + XGMAC_OMR);
  566. reg |= XGMAC_OMR_EFC;
  567. writel(reg, priv->base + XGMAC_OMR);
  568. } else {
  569. writel(0, priv->base + XGMAC_FLOW_CTRL);
  570. reg = readl(priv->base + XGMAC_OMR);
  571. reg &= ~XGMAC_OMR_EFC;
  572. writel(reg, priv->base + XGMAC_OMR);
  573. }
  574. return 0;
  575. }
  576. static void xgmac_rx_refill(struct xgmac_priv *priv)
  577. {
  578. struct xgmac_dma_desc *p;
  579. dma_addr_t paddr;
  580. while (dma_ring_space(priv->rx_head, priv->rx_tail, DMA_RX_RING_SZ) > 1) {
  581. int entry = priv->rx_head;
  582. struct sk_buff *skb;
  583. p = priv->dma_rx + entry;
  584. if (priv->rx_skbuff[entry] != NULL)
  585. continue;
  586. skb = __skb_dequeue(&priv->rx_recycle);
  587. if (skb == NULL)
  588. skb = netdev_alloc_skb(priv->dev, priv->dma_buf_sz);
  589. if (unlikely(skb == NULL))
  590. break;
  591. priv->rx_skbuff[entry] = skb;
  592. paddr = dma_map_single(priv->device, skb->data,
  593. priv->dma_buf_sz, DMA_FROM_DEVICE);
  594. desc_set_buf_addr(p, paddr, priv->dma_buf_sz);
  595. netdev_dbg(priv->dev, "rx ring: head %d, tail %d\n",
  596. priv->rx_head, priv->rx_tail);
  597. priv->rx_head = dma_ring_incr(priv->rx_head, DMA_RX_RING_SZ);
  598. /* Ensure descriptor is in memory before handing to h/w */
  599. wmb();
  600. desc_set_rx_owner(p);
  601. }
  602. }
  603. /**
  604. * init_xgmac_dma_desc_rings - init the RX/TX descriptor rings
  605. * @dev: net device structure
  606. * Description: this function initializes the DMA RX/TX descriptors
  607. * and allocates the socket buffers.
  608. */
  609. static int xgmac_dma_desc_rings_init(struct net_device *dev)
  610. {
  611. struct xgmac_priv *priv = netdev_priv(dev);
  612. unsigned int bfsize;
  613. /* Set the Buffer size according to the MTU;
  614. * indeed, in case of jumbo we need to bump-up the buffer sizes.
  615. */
  616. bfsize = ALIGN(dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN + 64,
  617. 64);
  618. netdev_dbg(priv->dev, "mtu [%d] bfsize [%d]\n", dev->mtu, bfsize);
  619. priv->rx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_RX_RING_SZ,
  620. GFP_KERNEL);
  621. if (!priv->rx_skbuff)
  622. return -ENOMEM;
  623. priv->dma_rx = dma_alloc_coherent(priv->device,
  624. DMA_RX_RING_SZ *
  625. sizeof(struct xgmac_dma_desc),
  626. &priv->dma_rx_phy,
  627. GFP_KERNEL);
  628. if (!priv->dma_rx)
  629. goto err_dma_rx;
  630. priv->tx_skbuff = kzalloc(sizeof(struct sk_buff *) * DMA_TX_RING_SZ,
  631. GFP_KERNEL);
  632. if (!priv->tx_skbuff)
  633. goto err_tx_skb;
  634. priv->dma_tx = dma_alloc_coherent(priv->device,
  635. DMA_TX_RING_SZ *
  636. sizeof(struct xgmac_dma_desc),
  637. &priv->dma_tx_phy,
  638. GFP_KERNEL);
  639. if (!priv->dma_tx)
  640. goto err_dma_tx;
  641. netdev_dbg(priv->dev, "DMA desc rings: virt addr (Rx %p, "
  642. "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
  643. priv->dma_rx, priv->dma_tx,
  644. (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
  645. priv->rx_tail = 0;
  646. priv->rx_head = 0;
  647. priv->dma_buf_sz = bfsize;
  648. desc_init_rx_desc(priv->dma_rx, DMA_RX_RING_SZ, priv->dma_buf_sz);
  649. xgmac_rx_refill(priv);
  650. priv->tx_tail = 0;
  651. priv->tx_head = 0;
  652. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  653. writel(priv->dma_tx_phy, priv->base + XGMAC_DMA_TX_BASE_ADDR);
  654. writel(priv->dma_rx_phy, priv->base + XGMAC_DMA_RX_BASE_ADDR);
  655. return 0;
  656. err_dma_tx:
  657. kfree(priv->tx_skbuff);
  658. err_tx_skb:
  659. dma_free_coherent(priv->device,
  660. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  661. priv->dma_rx, priv->dma_rx_phy);
  662. err_dma_rx:
  663. kfree(priv->rx_skbuff);
  664. return -ENOMEM;
  665. }
  666. static void xgmac_free_rx_skbufs(struct xgmac_priv *priv)
  667. {
  668. int i;
  669. struct xgmac_dma_desc *p;
  670. if (!priv->rx_skbuff)
  671. return;
  672. for (i = 0; i < DMA_RX_RING_SZ; i++) {
  673. if (priv->rx_skbuff[i] == NULL)
  674. continue;
  675. p = priv->dma_rx + i;
  676. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  677. priv->dma_buf_sz, DMA_FROM_DEVICE);
  678. dev_kfree_skb_any(priv->rx_skbuff[i]);
  679. priv->rx_skbuff[i] = NULL;
  680. }
  681. }
  682. static void xgmac_free_tx_skbufs(struct xgmac_priv *priv)
  683. {
  684. int i, f;
  685. struct xgmac_dma_desc *p;
  686. if (!priv->tx_skbuff)
  687. return;
  688. for (i = 0; i < DMA_TX_RING_SZ; i++) {
  689. if (priv->tx_skbuff[i] == NULL)
  690. continue;
  691. p = priv->dma_tx + i;
  692. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  693. desc_get_buf_len(p), DMA_TO_DEVICE);
  694. for (f = 0; f < skb_shinfo(priv->tx_skbuff[i])->nr_frags; f++) {
  695. p = priv->dma_tx + i++;
  696. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  697. desc_get_buf_len(p), DMA_TO_DEVICE);
  698. }
  699. dev_kfree_skb_any(priv->tx_skbuff[i]);
  700. priv->tx_skbuff[i] = NULL;
  701. }
  702. }
  703. static void xgmac_free_dma_desc_rings(struct xgmac_priv *priv)
  704. {
  705. /* Release the DMA TX/RX socket buffers */
  706. xgmac_free_rx_skbufs(priv);
  707. xgmac_free_tx_skbufs(priv);
  708. /* Free the consistent memory allocated for descriptor rings */
  709. if (priv->dma_tx) {
  710. dma_free_coherent(priv->device,
  711. DMA_TX_RING_SZ * sizeof(struct xgmac_dma_desc),
  712. priv->dma_tx, priv->dma_tx_phy);
  713. priv->dma_tx = NULL;
  714. }
  715. if (priv->dma_rx) {
  716. dma_free_coherent(priv->device,
  717. DMA_RX_RING_SZ * sizeof(struct xgmac_dma_desc),
  718. priv->dma_rx, priv->dma_rx_phy);
  719. priv->dma_rx = NULL;
  720. }
  721. kfree(priv->rx_skbuff);
  722. priv->rx_skbuff = NULL;
  723. kfree(priv->tx_skbuff);
  724. priv->tx_skbuff = NULL;
  725. }
  726. /**
  727. * xgmac_tx:
  728. * @priv: private driver structure
  729. * Description: it reclaims resources after transmission completes.
  730. */
  731. static void xgmac_tx_complete(struct xgmac_priv *priv)
  732. {
  733. int i;
  734. void __iomem *ioaddr = priv->base;
  735. writel(DMA_STATUS_TU | DMA_STATUS_NIS, ioaddr + XGMAC_DMA_STATUS);
  736. while (dma_ring_cnt(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ)) {
  737. unsigned int entry = priv->tx_tail;
  738. struct sk_buff *skb = priv->tx_skbuff[entry];
  739. struct xgmac_dma_desc *p = priv->dma_tx + entry;
  740. /* Check if the descriptor is owned by the DMA. */
  741. if (desc_get_owner(p))
  742. break;
  743. /* Verify tx error by looking at the last segment */
  744. if (desc_get_tx_ls(p))
  745. desc_get_tx_status(priv, p);
  746. netdev_dbg(priv->dev, "tx ring: curr %d, dirty %d\n",
  747. priv->tx_head, priv->tx_tail);
  748. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  749. desc_get_buf_len(p), DMA_TO_DEVICE);
  750. priv->tx_skbuff[entry] = NULL;
  751. priv->tx_tail = dma_ring_incr(entry, DMA_TX_RING_SZ);
  752. if (!skb) {
  753. continue;
  754. }
  755. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  756. entry = priv->tx_tail = dma_ring_incr(priv->tx_tail,
  757. DMA_TX_RING_SZ);
  758. p = priv->dma_tx + priv->tx_tail;
  759. dma_unmap_page(priv->device, desc_get_buf_addr(p),
  760. desc_get_buf_len(p), DMA_TO_DEVICE);
  761. }
  762. /*
  763. * If there's room in the queue (limit it to size)
  764. * we add this skb back into the pool,
  765. * if it's the right size.
  766. */
  767. if ((skb_queue_len(&priv->rx_recycle) <
  768. DMA_RX_RING_SZ) &&
  769. skb_recycle_check(skb, priv->dma_buf_sz))
  770. __skb_queue_head(&priv->rx_recycle, skb);
  771. else
  772. dev_kfree_skb(skb);
  773. }
  774. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) >
  775. TX_THRESH)
  776. netif_wake_queue(priv->dev);
  777. }
  778. /**
  779. * xgmac_tx_err:
  780. * @priv: pointer to the private device structure
  781. * Description: it cleans the descriptors and restarts the transmission
  782. * in case of errors.
  783. */
  784. static void xgmac_tx_err(struct xgmac_priv *priv)
  785. {
  786. u32 reg, value, inten;
  787. netif_stop_queue(priv->dev);
  788. inten = readl(priv->base + XGMAC_DMA_INTR_ENA);
  789. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  790. reg = readl(priv->base + XGMAC_DMA_CONTROL);
  791. writel(reg & ~DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  792. do {
  793. value = readl(priv->base + XGMAC_DMA_STATUS) & 0x700000;
  794. } while (value && (value != 0x600000));
  795. xgmac_free_tx_skbufs(priv);
  796. desc_init_tx_desc(priv->dma_tx, DMA_TX_RING_SZ);
  797. priv->tx_tail = 0;
  798. priv->tx_head = 0;
  799. writel(reg | DMA_CONTROL_ST, priv->base + XGMAC_DMA_CONTROL);
  800. writel(DMA_STATUS_TU | DMA_STATUS_TPS | DMA_STATUS_NIS | DMA_STATUS_AIS,
  801. priv->base + XGMAC_DMA_STATUS);
  802. writel(inten, priv->base + XGMAC_DMA_INTR_ENA);
  803. netif_wake_queue(priv->dev);
  804. }
  805. static int xgmac_hw_init(struct net_device *dev)
  806. {
  807. u32 value, ctrl;
  808. int limit;
  809. struct xgmac_priv *priv = netdev_priv(dev);
  810. void __iomem *ioaddr = priv->base;
  811. /* Save the ctrl register value */
  812. ctrl = readl(ioaddr + XGMAC_CONTROL) & XGMAC_CONTROL_SPD_MASK;
  813. /* SW reset */
  814. value = DMA_BUS_MODE_SFT_RESET;
  815. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  816. limit = 15000;
  817. while (limit-- &&
  818. (readl(ioaddr + XGMAC_DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  819. cpu_relax();
  820. if (limit < 0)
  821. return -EBUSY;
  822. value = (0x10 << DMA_BUS_MODE_PBL_SHIFT) |
  823. (0x10 << DMA_BUS_MODE_RPBL_SHIFT) |
  824. DMA_BUS_MODE_FB | DMA_BUS_MODE_ATDS | DMA_BUS_MODE_AAL;
  825. writel(value, ioaddr + XGMAC_DMA_BUS_MODE);
  826. /* Enable interrupts */
  827. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  828. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  829. /* XGMAC requires AXI bus init. This is a 'magic number' for now */
  830. writel(0x000100E, ioaddr + XGMAC_DMA_AXI_BUS);
  831. ctrl |= XGMAC_CONTROL_DDIC | XGMAC_CONTROL_JE | XGMAC_CONTROL_ACS |
  832. XGMAC_CONTROL_CAR;
  833. if (dev->features & NETIF_F_RXCSUM)
  834. ctrl |= XGMAC_CONTROL_IPC;
  835. writel(ctrl, ioaddr + XGMAC_CONTROL);
  836. value = DMA_CONTROL_DFF;
  837. writel(value, ioaddr + XGMAC_DMA_CONTROL);
  838. /* Set the HW DMA mode and the COE */
  839. writel(XGMAC_OMR_TSF | XGMAC_OMR_RSF | XGMAC_OMR_RFD | XGMAC_OMR_RFA,
  840. ioaddr + XGMAC_OMR);
  841. /* Reset the MMC counters */
  842. writel(1, ioaddr + XGMAC_MMC_CTRL);
  843. return 0;
  844. }
  845. /**
  846. * xgmac_open - open entry point of the driver
  847. * @dev : pointer to the device structure.
  848. * Description:
  849. * This function is the open entry point of the driver.
  850. * Return value:
  851. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  852. * file on failure.
  853. */
  854. static int xgmac_open(struct net_device *dev)
  855. {
  856. int ret;
  857. struct xgmac_priv *priv = netdev_priv(dev);
  858. void __iomem *ioaddr = priv->base;
  859. /* Check that the MAC address is valid. If its not, refuse
  860. * to bring the device up. The user must specify an
  861. * address using the following linux command:
  862. * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
  863. if (!is_valid_ether_addr(dev->dev_addr)) {
  864. random_ether_addr(dev->dev_addr);
  865. netdev_dbg(priv->dev, "generated random MAC address %pM\n",
  866. dev->dev_addr);
  867. }
  868. skb_queue_head_init(&priv->rx_recycle);
  869. memset(&priv->xstats, 0, sizeof(struct xgmac_extra_stats));
  870. /* Initialize the XGMAC and descriptors */
  871. xgmac_hw_init(dev);
  872. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  873. xgmac_set_flow_ctrl(priv, priv->rx_pause, priv->tx_pause);
  874. ret = xgmac_dma_desc_rings_init(dev);
  875. if (ret < 0)
  876. return ret;
  877. /* Enable the MAC Rx/Tx */
  878. xgmac_mac_enable(ioaddr);
  879. napi_enable(&priv->napi);
  880. netif_start_queue(dev);
  881. return 0;
  882. }
  883. /**
  884. * xgmac_release - close entry point of the driver
  885. * @dev : device pointer.
  886. * Description:
  887. * This is the stop entry point of the driver.
  888. */
  889. static int xgmac_stop(struct net_device *dev)
  890. {
  891. struct xgmac_priv *priv = netdev_priv(dev);
  892. netif_stop_queue(dev);
  893. if (readl(priv->base + XGMAC_DMA_INTR_ENA))
  894. napi_disable(&priv->napi);
  895. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  896. skb_queue_purge(&priv->rx_recycle);
  897. /* Disable the MAC core */
  898. xgmac_mac_disable(priv->base);
  899. /* Release and free the Rx/Tx resources */
  900. xgmac_free_dma_desc_rings(priv);
  901. return 0;
  902. }
  903. /**
  904. * xgmac_xmit:
  905. * @skb : the socket buffer
  906. * @dev : device pointer
  907. * Description : Tx entry point of the driver.
  908. */
  909. static netdev_tx_t xgmac_xmit(struct sk_buff *skb, struct net_device *dev)
  910. {
  911. struct xgmac_priv *priv = netdev_priv(dev);
  912. unsigned int entry;
  913. int i;
  914. int nfrags = skb_shinfo(skb)->nr_frags;
  915. struct xgmac_dma_desc *desc, *first;
  916. unsigned int desc_flags;
  917. unsigned int len;
  918. dma_addr_t paddr;
  919. if (dma_ring_space(priv->tx_head, priv->tx_tail, DMA_TX_RING_SZ) <
  920. (nfrags + 1)) {
  921. writel(DMA_INTR_DEFAULT_MASK | DMA_INTR_ENA_TIE,
  922. priv->base + XGMAC_DMA_INTR_ENA);
  923. netif_stop_queue(dev);
  924. return NETDEV_TX_BUSY;
  925. }
  926. desc_flags = (skb->ip_summed == CHECKSUM_PARTIAL) ?
  927. TXDESC_CSUM_ALL : 0;
  928. entry = priv->tx_head;
  929. desc = priv->dma_tx + entry;
  930. first = desc;
  931. len = skb_headlen(skb);
  932. paddr = dma_map_single(priv->device, skb->data, len, DMA_TO_DEVICE);
  933. if (dma_mapping_error(priv->device, paddr)) {
  934. dev_kfree_skb(skb);
  935. return -EIO;
  936. }
  937. priv->tx_skbuff[entry] = skb;
  938. desc_set_buf_addr_and_size(desc, paddr, len);
  939. for (i = 0; i < nfrags; i++) {
  940. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  941. len = frag->size;
  942. paddr = skb_frag_dma_map(priv->device, frag, 0, len,
  943. DMA_TO_DEVICE);
  944. if (dma_mapping_error(priv->device, paddr)) {
  945. dev_kfree_skb(skb);
  946. return -EIO;
  947. }
  948. entry = dma_ring_incr(entry, DMA_TX_RING_SZ);
  949. desc = priv->dma_tx + entry;
  950. priv->tx_skbuff[entry] = NULL;
  951. desc_set_buf_addr_and_size(desc, paddr, len);
  952. if (i < (nfrags - 1))
  953. desc_set_tx_owner(desc, desc_flags);
  954. }
  955. /* Interrupt on completition only for the latest segment */
  956. if (desc != first)
  957. desc_set_tx_owner(desc, desc_flags |
  958. TXDESC_LAST_SEG | TXDESC_INTERRUPT);
  959. else
  960. desc_flags |= TXDESC_LAST_SEG | TXDESC_INTERRUPT;
  961. /* Set owner on first desc last to avoid race condition */
  962. wmb();
  963. desc_set_tx_owner(first, desc_flags | TXDESC_FIRST_SEG);
  964. priv->tx_head = dma_ring_incr(entry, DMA_TX_RING_SZ);
  965. writel(1, priv->base + XGMAC_DMA_TX_POLL);
  966. return NETDEV_TX_OK;
  967. }
  968. static int xgmac_rx(struct xgmac_priv *priv, int limit)
  969. {
  970. unsigned int entry;
  971. unsigned int count = 0;
  972. struct xgmac_dma_desc *p;
  973. while (count < limit) {
  974. int ip_checksum;
  975. struct sk_buff *skb;
  976. int frame_len;
  977. writel(DMA_STATUS_RI | DMA_STATUS_NIS,
  978. priv->base + XGMAC_DMA_STATUS);
  979. entry = priv->rx_tail;
  980. p = priv->dma_rx + entry;
  981. if (desc_get_owner(p))
  982. break;
  983. count++;
  984. priv->rx_tail = dma_ring_incr(priv->rx_tail, DMA_RX_RING_SZ);
  985. /* read the status of the incoming frame */
  986. ip_checksum = desc_get_rx_status(priv, p);
  987. if (ip_checksum < 0)
  988. continue;
  989. skb = priv->rx_skbuff[entry];
  990. if (unlikely(!skb)) {
  991. netdev_err(priv->dev, "Inconsistent Rx descriptor chain\n");
  992. break;
  993. }
  994. priv->rx_skbuff[entry] = NULL;
  995. frame_len = desc_get_rx_frame_len(p);
  996. netdev_dbg(priv->dev, "RX frame size %d, COE status: %d\n",
  997. frame_len, ip_checksum);
  998. skb_put(skb, frame_len);
  999. dma_unmap_single(priv->device, desc_get_buf_addr(p),
  1000. frame_len, DMA_FROM_DEVICE);
  1001. skb->protocol = eth_type_trans(skb, priv->dev);
  1002. skb->ip_summed = ip_checksum;
  1003. if (ip_checksum == CHECKSUM_NONE)
  1004. netif_receive_skb(skb);
  1005. else
  1006. napi_gro_receive(&priv->napi, skb);
  1007. }
  1008. xgmac_rx_refill(priv);
  1009. writel(1, priv->base + XGMAC_DMA_RX_POLL);
  1010. return count;
  1011. }
  1012. /**
  1013. * xgmac_poll - xgmac poll method (NAPI)
  1014. * @napi : pointer to the napi structure.
  1015. * @budget : maximum number of packets that the current CPU can receive from
  1016. * all interfaces.
  1017. * Description :
  1018. * This function implements the the reception process.
  1019. * Also it runs the TX completion thread
  1020. */
  1021. static int xgmac_poll(struct napi_struct *napi, int budget)
  1022. {
  1023. struct xgmac_priv *priv = container_of(napi,
  1024. struct xgmac_priv, napi);
  1025. int work_done = 0;
  1026. xgmac_tx_complete(priv);
  1027. work_done = xgmac_rx(priv, budget);
  1028. if (work_done < budget) {
  1029. napi_complete(napi);
  1030. writel(DMA_INTR_DEFAULT_MASK, priv->base + XGMAC_DMA_INTR_ENA);
  1031. }
  1032. return work_done;
  1033. }
  1034. /**
  1035. * xgmac_tx_timeout
  1036. * @dev : Pointer to net device structure
  1037. * Description: this function is called when a packet transmission fails to
  1038. * complete within a reasonable tmrate. The driver will mark the error in the
  1039. * netdev structure and arrange for the device to be reset to a sane state
  1040. * in order to transmit a new packet.
  1041. */
  1042. static void xgmac_tx_timeout(struct net_device *dev)
  1043. {
  1044. struct xgmac_priv *priv = netdev_priv(dev);
  1045. /* Clear Tx resources and restart transmitting again */
  1046. xgmac_tx_err(priv);
  1047. }
  1048. /**
  1049. * xgmac_set_rx_mode - entry point for multicast addressing
  1050. * @dev : pointer to the device structure
  1051. * Description:
  1052. * This function is a driver entry point which gets called by the kernel
  1053. * whenever multicast addresses must be enabled/disabled.
  1054. * Return value:
  1055. * void.
  1056. */
  1057. static void xgmac_set_rx_mode(struct net_device *dev)
  1058. {
  1059. int i;
  1060. struct xgmac_priv *priv = netdev_priv(dev);
  1061. void __iomem *ioaddr = priv->base;
  1062. unsigned int value = 0;
  1063. u32 hash_filter[XGMAC_NUM_HASH];
  1064. int reg = 1;
  1065. struct netdev_hw_addr *ha;
  1066. bool use_hash = false;
  1067. netdev_dbg(priv->dev, "# mcasts %d, # unicast %d\n",
  1068. netdev_mc_count(dev), netdev_uc_count(dev));
  1069. if (dev->flags & IFF_PROMISC) {
  1070. writel(XGMAC_FRAME_FILTER_PR, ioaddr + XGMAC_FRAME_FILTER);
  1071. return;
  1072. }
  1073. memset(hash_filter, 0, sizeof(hash_filter));
  1074. if (netdev_uc_count(dev) > XGMAC_MAX_FILTER_ADDR) {
  1075. use_hash = true;
  1076. value |= XGMAC_FRAME_FILTER_HUC | XGMAC_FRAME_FILTER_HPF;
  1077. }
  1078. netdev_for_each_uc_addr(ha, dev) {
  1079. if (use_hash) {
  1080. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1081. /* The most significant 4 bits determine the register to
  1082. * use (H/L) while the other 5 bits determine the bit
  1083. * within the register. */
  1084. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1085. } else {
  1086. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1087. reg++;
  1088. }
  1089. }
  1090. if (dev->flags & IFF_ALLMULTI) {
  1091. value |= XGMAC_FRAME_FILTER_PM;
  1092. goto out;
  1093. }
  1094. if ((netdev_mc_count(dev) + reg - 1) > XGMAC_MAX_FILTER_ADDR) {
  1095. use_hash = true;
  1096. value |= XGMAC_FRAME_FILTER_HMC | XGMAC_FRAME_FILTER_HPF;
  1097. }
  1098. netdev_for_each_mc_addr(ha, dev) {
  1099. if (use_hash) {
  1100. u32 bit_nr = ~ether_crc(ETH_ALEN, ha->addr) >> 23;
  1101. /* The most significant 4 bits determine the register to
  1102. * use (H/L) while the other 5 bits determine the bit
  1103. * within the register. */
  1104. hash_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1105. } else {
  1106. xgmac_set_mac_addr(ioaddr, ha->addr, reg);
  1107. reg++;
  1108. }
  1109. }
  1110. out:
  1111. for (i = 0; i < XGMAC_NUM_HASH; i++)
  1112. writel(hash_filter[i], ioaddr + XGMAC_HASH(i));
  1113. writel(value, ioaddr + XGMAC_FRAME_FILTER);
  1114. }
  1115. /**
  1116. * xgmac_change_mtu - entry point to change MTU size for the device.
  1117. * @dev : device pointer.
  1118. * @new_mtu : the new MTU size for the device.
  1119. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1120. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1121. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1122. * Return value:
  1123. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1124. * file on failure.
  1125. */
  1126. static int xgmac_change_mtu(struct net_device *dev, int new_mtu)
  1127. {
  1128. struct xgmac_priv *priv = netdev_priv(dev);
  1129. int old_mtu;
  1130. if ((new_mtu < 46) || (new_mtu > MAX_MTU)) {
  1131. netdev_err(priv->dev, "invalid MTU, max MTU is: %d\n", MAX_MTU);
  1132. return -EINVAL;
  1133. }
  1134. old_mtu = dev->mtu;
  1135. dev->mtu = new_mtu;
  1136. /* return early if the buffer sizes will not change */
  1137. if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
  1138. return 0;
  1139. if (old_mtu == new_mtu)
  1140. return 0;
  1141. /* Stop everything, get ready to change the MTU */
  1142. if (!netif_running(dev))
  1143. return 0;
  1144. /* Bring the interface down and then back up */
  1145. xgmac_stop(dev);
  1146. return xgmac_open(dev);
  1147. }
  1148. static irqreturn_t xgmac_pmt_interrupt(int irq, void *dev_id)
  1149. {
  1150. u32 intr_status;
  1151. struct net_device *dev = (struct net_device *)dev_id;
  1152. struct xgmac_priv *priv = netdev_priv(dev);
  1153. void __iomem *ioaddr = priv->base;
  1154. intr_status = readl(ioaddr + XGMAC_INT_STAT);
  1155. if (intr_status & XGMAC_INT_STAT_PMT) {
  1156. netdev_dbg(priv->dev, "received Magic frame\n");
  1157. /* clear the PMT bits 5 and 6 by reading the PMT */
  1158. readl(ioaddr + XGMAC_PMT);
  1159. }
  1160. return IRQ_HANDLED;
  1161. }
  1162. static irqreturn_t xgmac_interrupt(int irq, void *dev_id)
  1163. {
  1164. u32 intr_status;
  1165. bool tx_err = false;
  1166. struct net_device *dev = (struct net_device *)dev_id;
  1167. struct xgmac_priv *priv = netdev_priv(dev);
  1168. struct xgmac_extra_stats *x = &priv->xstats;
  1169. /* read the status register (CSR5) */
  1170. intr_status = readl(priv->base + XGMAC_DMA_STATUS);
  1171. intr_status &= readl(priv->base + XGMAC_DMA_INTR_ENA);
  1172. writel(intr_status, priv->base + XGMAC_DMA_STATUS);
  1173. /* It displays the DMA process states (CSR5 register) */
  1174. /* ABNORMAL interrupts */
  1175. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  1176. if (intr_status & DMA_STATUS_TJT) {
  1177. netdev_err(priv->dev, "transmit jabber\n");
  1178. x->tx_jabber++;
  1179. }
  1180. if (intr_status & DMA_STATUS_RU)
  1181. x->rx_buf_unav++;
  1182. if (intr_status & DMA_STATUS_RPS) {
  1183. netdev_err(priv->dev, "receive process stopped\n");
  1184. x->rx_process_stopped++;
  1185. }
  1186. if (intr_status & DMA_STATUS_ETI) {
  1187. netdev_err(priv->dev, "transmit early interrupt\n");
  1188. x->tx_early++;
  1189. }
  1190. if (intr_status & DMA_STATUS_TPS) {
  1191. netdev_err(priv->dev, "transmit process stopped\n");
  1192. x->tx_process_stopped++;
  1193. tx_err = true;
  1194. }
  1195. if (intr_status & DMA_STATUS_FBI) {
  1196. netdev_err(priv->dev, "fatal bus error\n");
  1197. x->fatal_bus_error++;
  1198. tx_err = true;
  1199. }
  1200. if (tx_err)
  1201. xgmac_tx_err(priv);
  1202. }
  1203. /* TX/RX NORMAL interrupts */
  1204. if (intr_status & (DMA_STATUS_RI | DMA_STATUS_TU)) {
  1205. writel(DMA_INTR_ABNORMAL, priv->base + XGMAC_DMA_INTR_ENA);
  1206. napi_schedule(&priv->napi);
  1207. }
  1208. return IRQ_HANDLED;
  1209. }
  1210. #ifdef CONFIG_NET_POLL_CONTROLLER
  1211. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1212. * to allow network I/O with interrupts disabled. */
  1213. static void xgmac_poll_controller(struct net_device *dev)
  1214. {
  1215. disable_irq(dev->irq);
  1216. xgmac_interrupt(dev->irq, dev);
  1217. enable_irq(dev->irq);
  1218. }
  1219. #endif
  1220. static struct rtnl_link_stats64 *
  1221. xgmac_get_stats64(struct net_device *dev,
  1222. struct rtnl_link_stats64 *storage)
  1223. {
  1224. struct xgmac_priv *priv = netdev_priv(dev);
  1225. void __iomem *base = priv->base;
  1226. u32 count;
  1227. spin_lock_bh(&priv->stats_lock);
  1228. writel(XGMAC_MMC_CTRL_CNT_FRZ, base + XGMAC_MMC_CTRL);
  1229. storage->rx_bytes = readl(base + XGMAC_MMC_RXOCTET_G_LO);
  1230. storage->rx_bytes |= (u64)(readl(base + XGMAC_MMC_RXOCTET_G_HI)) << 32;
  1231. storage->rx_packets = readl(base + XGMAC_MMC_RXFRAME_GB_LO);
  1232. storage->multicast = readl(base + XGMAC_MMC_RXMCFRAME_G);
  1233. storage->rx_crc_errors = readl(base + XGMAC_MMC_RXCRCERR);
  1234. storage->rx_length_errors = readl(base + XGMAC_MMC_RXLENGTHERR);
  1235. storage->rx_missed_errors = readl(base + XGMAC_MMC_RXOVERFLOW);
  1236. storage->tx_bytes = readl(base + XGMAC_MMC_TXOCTET_G_LO);
  1237. storage->tx_bytes |= (u64)(readl(base + XGMAC_MMC_TXOCTET_G_HI)) << 32;
  1238. count = readl(base + XGMAC_MMC_TXFRAME_GB_LO);
  1239. storage->tx_errors = count - readl(base + XGMAC_MMC_TXFRAME_G_LO);
  1240. storage->tx_packets = count;
  1241. storage->tx_fifo_errors = readl(base + XGMAC_MMC_TXUNDERFLOW);
  1242. writel(0, base + XGMAC_MMC_CTRL);
  1243. spin_unlock_bh(&priv->stats_lock);
  1244. return storage;
  1245. }
  1246. static int xgmac_set_mac_address(struct net_device *dev, void *p)
  1247. {
  1248. struct xgmac_priv *priv = netdev_priv(dev);
  1249. void __iomem *ioaddr = priv->base;
  1250. struct sockaddr *addr = p;
  1251. if (!is_valid_ether_addr(addr->sa_data))
  1252. return -EADDRNOTAVAIL;
  1253. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1254. xgmac_set_mac_addr(ioaddr, dev->dev_addr, 0);
  1255. return 0;
  1256. }
  1257. static int xgmac_set_features(struct net_device *dev, netdev_features_t features)
  1258. {
  1259. u32 ctrl;
  1260. struct xgmac_priv *priv = netdev_priv(dev);
  1261. void __iomem *ioaddr = priv->base;
  1262. u32 changed = dev->features ^ features;
  1263. if (!(changed & NETIF_F_RXCSUM))
  1264. return 0;
  1265. ctrl = readl(ioaddr + XGMAC_CONTROL);
  1266. if (features & NETIF_F_RXCSUM)
  1267. ctrl |= XGMAC_CONTROL_IPC;
  1268. else
  1269. ctrl &= ~XGMAC_CONTROL_IPC;
  1270. writel(ctrl, ioaddr + XGMAC_CONTROL);
  1271. return 0;
  1272. }
  1273. static const struct net_device_ops xgmac_netdev_ops = {
  1274. .ndo_open = xgmac_open,
  1275. .ndo_start_xmit = xgmac_xmit,
  1276. .ndo_stop = xgmac_stop,
  1277. .ndo_change_mtu = xgmac_change_mtu,
  1278. .ndo_set_rx_mode = xgmac_set_rx_mode,
  1279. .ndo_tx_timeout = xgmac_tx_timeout,
  1280. .ndo_get_stats64 = xgmac_get_stats64,
  1281. #ifdef CONFIG_NET_POLL_CONTROLLER
  1282. .ndo_poll_controller = xgmac_poll_controller,
  1283. #endif
  1284. .ndo_set_mac_address = xgmac_set_mac_address,
  1285. .ndo_set_features = xgmac_set_features,
  1286. };
  1287. static int xgmac_ethtool_getsettings(struct net_device *dev,
  1288. struct ethtool_cmd *cmd)
  1289. {
  1290. cmd->autoneg = 0;
  1291. cmd->duplex = DUPLEX_FULL;
  1292. ethtool_cmd_speed_set(cmd, 10000);
  1293. cmd->supported = 0;
  1294. cmd->advertising = 0;
  1295. cmd->transceiver = XCVR_INTERNAL;
  1296. return 0;
  1297. }
  1298. static void xgmac_get_pauseparam(struct net_device *netdev,
  1299. struct ethtool_pauseparam *pause)
  1300. {
  1301. struct xgmac_priv *priv = netdev_priv(netdev);
  1302. pause->rx_pause = priv->rx_pause;
  1303. pause->tx_pause = priv->tx_pause;
  1304. }
  1305. static int xgmac_set_pauseparam(struct net_device *netdev,
  1306. struct ethtool_pauseparam *pause)
  1307. {
  1308. struct xgmac_priv *priv = netdev_priv(netdev);
  1309. if (pause->autoneg)
  1310. return -EINVAL;
  1311. return xgmac_set_flow_ctrl(priv, pause->rx_pause, pause->tx_pause);
  1312. }
  1313. struct xgmac_stats {
  1314. char stat_string[ETH_GSTRING_LEN];
  1315. int stat_offset;
  1316. bool is_reg;
  1317. };
  1318. #define XGMAC_STAT(m) \
  1319. { #m, offsetof(struct xgmac_priv, xstats.m), false }
  1320. #define XGMAC_HW_STAT(m, reg_offset) \
  1321. { #m, reg_offset, true }
  1322. static const struct xgmac_stats xgmac_gstrings_stats[] = {
  1323. XGMAC_STAT(tx_frame_flushed),
  1324. XGMAC_STAT(tx_payload_error),
  1325. XGMAC_STAT(tx_ip_header_error),
  1326. XGMAC_STAT(tx_local_fault),
  1327. XGMAC_STAT(tx_remote_fault),
  1328. XGMAC_STAT(tx_early),
  1329. XGMAC_STAT(tx_process_stopped),
  1330. XGMAC_STAT(tx_jabber),
  1331. XGMAC_STAT(rx_buf_unav),
  1332. XGMAC_STAT(rx_process_stopped),
  1333. XGMAC_STAT(rx_payload_error),
  1334. XGMAC_STAT(rx_ip_header_error),
  1335. XGMAC_STAT(rx_da_filter_fail),
  1336. XGMAC_STAT(rx_sa_filter_fail),
  1337. XGMAC_STAT(fatal_bus_error),
  1338. XGMAC_HW_STAT(rx_watchdog, XGMAC_MMC_RXWATCHDOG),
  1339. XGMAC_HW_STAT(tx_vlan, XGMAC_MMC_TXVLANFRAME),
  1340. XGMAC_HW_STAT(rx_vlan, XGMAC_MMC_RXVLANFRAME),
  1341. XGMAC_HW_STAT(tx_pause, XGMAC_MMC_TXPAUSEFRAME),
  1342. XGMAC_HW_STAT(rx_pause, XGMAC_MMC_RXPAUSEFRAME),
  1343. };
  1344. #define XGMAC_STATS_LEN ARRAY_SIZE(xgmac_gstrings_stats)
  1345. static void xgmac_get_ethtool_stats(struct net_device *dev,
  1346. struct ethtool_stats *dummy,
  1347. u64 *data)
  1348. {
  1349. struct xgmac_priv *priv = netdev_priv(dev);
  1350. void *p = priv;
  1351. int i;
  1352. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1353. if (xgmac_gstrings_stats[i].is_reg)
  1354. *data++ = readl(priv->base +
  1355. xgmac_gstrings_stats[i].stat_offset);
  1356. else
  1357. *data++ = *(u32 *)(p +
  1358. xgmac_gstrings_stats[i].stat_offset);
  1359. }
  1360. }
  1361. static int xgmac_get_sset_count(struct net_device *netdev, int sset)
  1362. {
  1363. switch (sset) {
  1364. case ETH_SS_STATS:
  1365. return XGMAC_STATS_LEN;
  1366. default:
  1367. return -EINVAL;
  1368. }
  1369. }
  1370. static void xgmac_get_strings(struct net_device *dev, u32 stringset,
  1371. u8 *data)
  1372. {
  1373. int i;
  1374. u8 *p = data;
  1375. switch (stringset) {
  1376. case ETH_SS_STATS:
  1377. for (i = 0; i < XGMAC_STATS_LEN; i++) {
  1378. memcpy(p, xgmac_gstrings_stats[i].stat_string,
  1379. ETH_GSTRING_LEN);
  1380. p += ETH_GSTRING_LEN;
  1381. }
  1382. break;
  1383. default:
  1384. WARN_ON(1);
  1385. break;
  1386. }
  1387. }
  1388. static void xgmac_get_wol(struct net_device *dev,
  1389. struct ethtool_wolinfo *wol)
  1390. {
  1391. struct xgmac_priv *priv = netdev_priv(dev);
  1392. if (device_can_wakeup(priv->device)) {
  1393. wol->supported = WAKE_MAGIC | WAKE_UCAST;
  1394. wol->wolopts = priv->wolopts;
  1395. }
  1396. }
  1397. static int xgmac_set_wol(struct net_device *dev,
  1398. struct ethtool_wolinfo *wol)
  1399. {
  1400. struct xgmac_priv *priv = netdev_priv(dev);
  1401. u32 support = WAKE_MAGIC | WAKE_UCAST;
  1402. if (!device_can_wakeup(priv->device))
  1403. return -ENOTSUPP;
  1404. if (wol->wolopts & ~support)
  1405. return -EINVAL;
  1406. priv->wolopts = wol->wolopts;
  1407. if (wol->wolopts) {
  1408. device_set_wakeup_enable(priv->device, 1);
  1409. enable_irq_wake(dev->irq);
  1410. } else {
  1411. device_set_wakeup_enable(priv->device, 0);
  1412. disable_irq_wake(dev->irq);
  1413. }
  1414. return 0;
  1415. }
  1416. static const struct ethtool_ops xgmac_ethtool_ops = {
  1417. .get_settings = xgmac_ethtool_getsettings,
  1418. .get_link = ethtool_op_get_link,
  1419. .get_pauseparam = xgmac_get_pauseparam,
  1420. .set_pauseparam = xgmac_set_pauseparam,
  1421. .get_ethtool_stats = xgmac_get_ethtool_stats,
  1422. .get_strings = xgmac_get_strings,
  1423. .get_wol = xgmac_get_wol,
  1424. .set_wol = xgmac_set_wol,
  1425. .get_sset_count = xgmac_get_sset_count,
  1426. };
  1427. /**
  1428. * xgmac_probe
  1429. * @pdev: platform device pointer
  1430. * Description: the driver is initialized through platform_device.
  1431. */
  1432. static int xgmac_probe(struct platform_device *pdev)
  1433. {
  1434. int ret = 0;
  1435. struct resource *res;
  1436. struct net_device *ndev = NULL;
  1437. struct xgmac_priv *priv = NULL;
  1438. u32 uid;
  1439. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1440. if (!res)
  1441. return -ENODEV;
  1442. if (!request_mem_region(res->start, resource_size(res), pdev->name))
  1443. return -EBUSY;
  1444. ndev = alloc_etherdev(sizeof(struct xgmac_priv));
  1445. if (!ndev) {
  1446. ret = -ENOMEM;
  1447. goto err_alloc;
  1448. }
  1449. SET_NETDEV_DEV(ndev, &pdev->dev);
  1450. priv = netdev_priv(ndev);
  1451. platform_set_drvdata(pdev, ndev);
  1452. ether_setup(ndev);
  1453. ndev->netdev_ops = &xgmac_netdev_ops;
  1454. SET_ETHTOOL_OPS(ndev, &xgmac_ethtool_ops);
  1455. spin_lock_init(&priv->stats_lock);
  1456. priv->device = &pdev->dev;
  1457. priv->dev = ndev;
  1458. priv->rx_pause = 1;
  1459. priv->tx_pause = 1;
  1460. priv->base = ioremap(res->start, resource_size(res));
  1461. if (!priv->base) {
  1462. netdev_err(ndev, "ioremap failed\n");
  1463. ret = -ENOMEM;
  1464. goto err_io;
  1465. }
  1466. uid = readl(priv->base + XGMAC_VERSION);
  1467. netdev_info(ndev, "h/w version is 0x%x\n", uid);
  1468. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1469. ndev->irq = platform_get_irq(pdev, 0);
  1470. if (ndev->irq == -ENXIO) {
  1471. netdev_err(ndev, "No irq resource\n");
  1472. ret = ndev->irq;
  1473. goto err_irq;
  1474. }
  1475. ret = request_irq(ndev->irq, xgmac_interrupt, 0,
  1476. dev_name(&pdev->dev), ndev);
  1477. if (ret < 0) {
  1478. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1479. ndev->irq, ret);
  1480. goto err_irq;
  1481. }
  1482. priv->pmt_irq = platform_get_irq(pdev, 1);
  1483. if (priv->pmt_irq == -ENXIO) {
  1484. netdev_err(ndev, "No pmt irq resource\n");
  1485. ret = priv->pmt_irq;
  1486. goto err_pmt_irq;
  1487. }
  1488. ret = request_irq(priv->pmt_irq, xgmac_pmt_interrupt, 0,
  1489. dev_name(&pdev->dev), ndev);
  1490. if (ret < 0) {
  1491. netdev_err(ndev, "Could not request irq %d - ret %d)\n",
  1492. priv->pmt_irq, ret);
  1493. goto err_pmt_irq;
  1494. }
  1495. device_set_wakeup_capable(&pdev->dev, 1);
  1496. if (device_can_wakeup(priv->device))
  1497. priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
  1498. ndev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  1499. if (readl(priv->base + XGMAC_DMA_HW_FEATURE) & DMA_HW_FEAT_TXCOESEL)
  1500. ndev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1501. NETIF_F_RXCSUM;
  1502. ndev->features |= ndev->hw_features;
  1503. ndev->priv_flags |= IFF_UNICAST_FLT;
  1504. /* Get the MAC address */
  1505. xgmac_get_mac_addr(priv->base, ndev->dev_addr, 0);
  1506. if (!is_valid_ether_addr(ndev->dev_addr))
  1507. netdev_warn(ndev, "MAC address %pM not valid",
  1508. ndev->dev_addr);
  1509. netif_napi_add(ndev, &priv->napi, xgmac_poll, 64);
  1510. ret = register_netdev(ndev);
  1511. if (ret)
  1512. goto err_reg;
  1513. return 0;
  1514. err_reg:
  1515. netif_napi_del(&priv->napi);
  1516. free_irq(priv->pmt_irq, ndev);
  1517. err_pmt_irq:
  1518. free_irq(ndev->irq, ndev);
  1519. err_irq:
  1520. iounmap(priv->base);
  1521. err_io:
  1522. free_netdev(ndev);
  1523. err_alloc:
  1524. release_mem_region(res->start, resource_size(res));
  1525. platform_set_drvdata(pdev, NULL);
  1526. return ret;
  1527. }
  1528. /**
  1529. * xgmac_dvr_remove
  1530. * @pdev: platform device pointer
  1531. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1532. * changes the link status, releases the DMA descriptor rings,
  1533. * unregisters the MDIO bus and unmaps the allocated memory.
  1534. */
  1535. static int xgmac_remove(struct platform_device *pdev)
  1536. {
  1537. struct net_device *ndev = platform_get_drvdata(pdev);
  1538. struct xgmac_priv *priv = netdev_priv(ndev);
  1539. struct resource *res;
  1540. xgmac_mac_disable(priv->base);
  1541. /* Free the IRQ lines */
  1542. free_irq(ndev->irq, ndev);
  1543. free_irq(priv->pmt_irq, ndev);
  1544. platform_set_drvdata(pdev, NULL);
  1545. unregister_netdev(ndev);
  1546. netif_napi_del(&priv->napi);
  1547. iounmap(priv->base);
  1548. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1549. release_mem_region(res->start, resource_size(res));
  1550. free_netdev(ndev);
  1551. return 0;
  1552. }
  1553. #ifdef CONFIG_PM_SLEEP
  1554. static void xgmac_pmt(void __iomem *ioaddr, unsigned long mode)
  1555. {
  1556. unsigned int pmt = 0;
  1557. if (mode & WAKE_MAGIC)
  1558. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_MAGIC_PKT;
  1559. if (mode & WAKE_UCAST)
  1560. pmt |= XGMAC_PMT_POWERDOWN | XGMAC_PMT_GLBL_UNICAST;
  1561. writel(pmt, ioaddr + XGMAC_PMT);
  1562. }
  1563. static int xgmac_suspend(struct device *dev)
  1564. {
  1565. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1566. struct xgmac_priv *priv = netdev_priv(ndev);
  1567. u32 value;
  1568. if (!ndev || !netif_running(ndev))
  1569. return 0;
  1570. netif_device_detach(ndev);
  1571. napi_disable(&priv->napi);
  1572. writel(0, priv->base + XGMAC_DMA_INTR_ENA);
  1573. if (device_may_wakeup(priv->device)) {
  1574. /* Stop TX/RX DMA Only */
  1575. value = readl(priv->base + XGMAC_DMA_CONTROL);
  1576. value &= ~(DMA_CONTROL_ST | DMA_CONTROL_SR);
  1577. writel(value, priv->base + XGMAC_DMA_CONTROL);
  1578. xgmac_pmt(priv->base, priv->wolopts);
  1579. } else
  1580. xgmac_mac_disable(priv->base);
  1581. return 0;
  1582. }
  1583. static int xgmac_resume(struct device *dev)
  1584. {
  1585. struct net_device *ndev = platform_get_drvdata(to_platform_device(dev));
  1586. struct xgmac_priv *priv = netdev_priv(ndev);
  1587. void __iomem *ioaddr = priv->base;
  1588. if (!netif_running(ndev))
  1589. return 0;
  1590. xgmac_pmt(ioaddr, 0);
  1591. /* Enable the MAC and DMA */
  1592. xgmac_mac_enable(ioaddr);
  1593. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_STATUS);
  1594. writel(DMA_INTR_DEFAULT_MASK, ioaddr + XGMAC_DMA_INTR_ENA);
  1595. netif_device_attach(ndev);
  1596. napi_enable(&priv->napi);
  1597. return 0;
  1598. }
  1599. static SIMPLE_DEV_PM_OPS(xgmac_pm_ops, xgmac_suspend, xgmac_resume);
  1600. #define XGMAC_PM_OPS (&xgmac_pm_ops)
  1601. #else
  1602. #define XGMAC_PM_OPS NULL
  1603. #endif /* CONFIG_PM_SLEEP */
  1604. static const struct of_device_id xgmac_of_match[] = {
  1605. { .compatible = "calxeda,hb-xgmac", },
  1606. {},
  1607. };
  1608. MODULE_DEVICE_TABLE(of, xgmac_of_match);
  1609. static struct platform_driver xgmac_driver = {
  1610. .driver = {
  1611. .name = "calxedaxgmac",
  1612. .of_match_table = xgmac_of_match,
  1613. },
  1614. .probe = xgmac_probe,
  1615. .remove = xgmac_remove,
  1616. .driver.pm = XGMAC_PM_OPS,
  1617. };
  1618. module_platform_driver(xgmac_driver);
  1619. MODULE_AUTHOR("Calxeda, Inc.");
  1620. MODULE_DESCRIPTION("Calxeda 10G XGMAC driver");
  1621. MODULE_LICENSE("GPL v2");