xgmac.c 55 KB

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