xgmac.c 55 KB

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