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