xref: /openbmc/linux/drivers/net/ethernet/qualcomm/emac/emac-mac.c (revision 4a7a3860caac1a8779e8c459d8abe21b111798d6)
1b9b17debSTimur Tabi /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
2b9b17debSTimur Tabi  *
3b9b17debSTimur Tabi  * This program is free software; you can redistribute it and/or modify
4b9b17debSTimur Tabi  * it under the terms of the GNU General Public License version 2 and
5b9b17debSTimur Tabi  * only version 2 as published by the Free Software Foundation.
6b9b17debSTimur Tabi  *
7b9b17debSTimur Tabi  * This program is distributed in the hope that it will be useful,
8b9b17debSTimur Tabi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9b9b17debSTimur Tabi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10b9b17debSTimur Tabi  * GNU General Public License for more details.
11b9b17debSTimur Tabi  */
12b9b17debSTimur Tabi 
13b9b17debSTimur Tabi /* Qualcomm Technologies, Inc. EMAC Ethernet Controller MAC layer support
14b9b17debSTimur Tabi  */
15b9b17debSTimur Tabi 
16b9b17debSTimur Tabi #include <linux/tcp.h>
17b9b17debSTimur Tabi #include <linux/ip.h>
18b9b17debSTimur Tabi #include <linux/ipv6.h>
19b9b17debSTimur Tabi #include <linux/crc32.h>
20b9b17debSTimur Tabi #include <linux/if_vlan.h>
21b9b17debSTimur Tabi #include <linux/jiffies.h>
22b9b17debSTimur Tabi #include <linux/phy.h>
23b9b17debSTimur Tabi #include <linux/of.h>
24b9b17debSTimur Tabi #include <net/ip6_checksum.h>
25b9b17debSTimur Tabi #include "emac.h"
26b9b17debSTimur Tabi #include "emac-sgmii.h"
27b9b17debSTimur Tabi 
28b9b17debSTimur Tabi /* EMAC_MAC_CTRL */
29b9b17debSTimur Tabi #define SINGLE_PAUSE_MODE       	0x10000000
30b9b17debSTimur Tabi #define DEBUG_MODE                      0x08000000
31b9b17debSTimur Tabi #define BROAD_EN                        0x04000000
32b9b17debSTimur Tabi #define MULTI_ALL                       0x02000000
33b9b17debSTimur Tabi #define RX_CHKSUM_EN                    0x01000000
34b9b17debSTimur Tabi #define HUGE                            0x00800000
35b9b17debSTimur Tabi #define SPEED(x)			(((x) & 0x3) << 20)
36b9b17debSTimur Tabi #define SPEED_MASK			SPEED(0x3)
37b9b17debSTimur Tabi #define SIMR                            0x00080000
38b9b17debSTimur Tabi #define TPAUSE                          0x00010000
39b9b17debSTimur Tabi #define PROM_MODE                       0x00008000
40b9b17debSTimur Tabi #define VLAN_STRIP                      0x00004000
41b9b17debSTimur Tabi #define PRLEN_BMSK                      0x00003c00
42b9b17debSTimur Tabi #define PRLEN_SHFT                      10
43b9b17debSTimur Tabi #define HUGEN                           0x00000200
44b9b17debSTimur Tabi #define FLCHK                           0x00000100
45b9b17debSTimur Tabi #define PCRCE                           0x00000080
46b9b17debSTimur Tabi #define CRCE                            0x00000040
47b9b17debSTimur Tabi #define FULLD                           0x00000020
48b9b17debSTimur Tabi #define MAC_LP_EN                       0x00000010
49b9b17debSTimur Tabi #define RXFC                            0x00000008
50b9b17debSTimur Tabi #define TXFC                            0x00000004
51b9b17debSTimur Tabi #define RXEN                            0x00000002
52b9b17debSTimur Tabi #define TXEN                            0x00000001
53b9b17debSTimur Tabi 
54b9b17debSTimur Tabi /* EMAC_DESC_CTRL_3 */
55b9b17debSTimur Tabi #define RFD_RING_SIZE_BMSK                                       0xfff
56b9b17debSTimur Tabi 
57b9b17debSTimur Tabi /* EMAC_DESC_CTRL_4 */
58b9b17debSTimur Tabi #define RX_BUFFER_SIZE_BMSK                                     0xffff
59b9b17debSTimur Tabi 
60b9b17debSTimur Tabi /* EMAC_DESC_CTRL_6 */
61b9b17debSTimur Tabi #define RRD_RING_SIZE_BMSK                                       0xfff
62b9b17debSTimur Tabi 
63b9b17debSTimur Tabi /* EMAC_DESC_CTRL_9 */
64b9b17debSTimur Tabi #define TPD_RING_SIZE_BMSK                                      0xffff
65b9b17debSTimur Tabi 
66b9b17debSTimur Tabi /* EMAC_TXQ_CTRL_0 */
67b9b17debSTimur Tabi #define NUM_TXF_BURST_PREF_BMSK                             0xffff0000
68b9b17debSTimur Tabi #define NUM_TXF_BURST_PREF_SHFT                                     16
69b9b17debSTimur Tabi #define LS_8023_SP                                                0x80
70b9b17debSTimur Tabi #define TXQ_MODE                                                  0x40
71b9b17debSTimur Tabi #define TXQ_EN                                                    0x20
72b9b17debSTimur Tabi #define IP_OP_SP                                                  0x10
73b9b17debSTimur Tabi #define NUM_TPD_BURST_PREF_BMSK                                    0xf
74b9b17debSTimur Tabi #define NUM_TPD_BURST_PREF_SHFT                                      0
75b9b17debSTimur Tabi 
76b9b17debSTimur Tabi /* EMAC_TXQ_CTRL_1 */
77b9b17debSTimur Tabi #define JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK                        0x7ff
78b9b17debSTimur Tabi 
79b9b17debSTimur Tabi /* EMAC_TXQ_CTRL_2 */
80b9b17debSTimur Tabi #define TXF_HWM_BMSK                                         0xfff0000
81b9b17debSTimur Tabi #define TXF_LWM_BMSK                                             0xfff
82b9b17debSTimur Tabi 
83b9b17debSTimur Tabi /* EMAC_RXQ_CTRL_0 */
84b9b17debSTimur Tabi #define RXQ_EN                                                 BIT(31)
85b9b17debSTimur Tabi #define CUT_THRU_EN                                            BIT(30)
86b9b17debSTimur Tabi #define RSS_HASH_EN                                            BIT(29)
87b9b17debSTimur Tabi #define NUM_RFD_BURST_PREF_BMSK                              0x3f00000
88b9b17debSTimur Tabi #define NUM_RFD_BURST_PREF_SHFT                                     20
89b9b17debSTimur Tabi #define IDT_TABLE_SIZE_BMSK                                    0x1ff00
90b9b17debSTimur Tabi #define IDT_TABLE_SIZE_SHFT                                          8
91b9b17debSTimur Tabi #define SP_IPV6                                                   0x80
92b9b17debSTimur Tabi 
93b9b17debSTimur Tabi /* EMAC_RXQ_CTRL_1 */
94b9b17debSTimur Tabi #define JUMBO_1KAH_BMSK                                         0xf000
95b9b17debSTimur Tabi #define JUMBO_1KAH_SHFT                                             12
96b9b17debSTimur Tabi #define RFD_PREF_LOW_TH                                           0x10
97b9b17debSTimur Tabi #define RFD_PREF_LOW_THRESHOLD_BMSK                              0xfc0
98b9b17debSTimur Tabi #define RFD_PREF_LOW_THRESHOLD_SHFT                                  6
99b9b17debSTimur Tabi #define RFD_PREF_UP_TH                                            0x10
100b9b17debSTimur Tabi #define RFD_PREF_UP_THRESHOLD_BMSK                                0x3f
101b9b17debSTimur Tabi #define RFD_PREF_UP_THRESHOLD_SHFT                                   0
102b9b17debSTimur Tabi 
103b9b17debSTimur Tabi /* EMAC_RXQ_CTRL_2 */
104b9b17debSTimur Tabi #define RXF_DOF_THRESFHOLD                                       0x1a0
105b9b17debSTimur Tabi #define RXF_DOF_THRESHOLD_BMSK                               0xfff0000
106b9b17debSTimur Tabi #define RXF_DOF_THRESHOLD_SHFT                                      16
107b9b17debSTimur Tabi #define RXF_UOF_THRESFHOLD                                        0xbe
108b9b17debSTimur Tabi #define RXF_UOF_THRESHOLD_BMSK                                   0xfff
109b9b17debSTimur Tabi #define RXF_UOF_THRESHOLD_SHFT                                       0
110b9b17debSTimur Tabi 
111b9b17debSTimur Tabi /* EMAC_RXQ_CTRL_3 */
112b9b17debSTimur Tabi #define RXD_TIMER_BMSK                                      0xffff0000
113b9b17debSTimur Tabi #define RXD_THRESHOLD_BMSK                                       0xfff
114b9b17debSTimur Tabi #define RXD_THRESHOLD_SHFT                                           0
115b9b17debSTimur Tabi 
116b9b17debSTimur Tabi /* EMAC_DMA_CTRL */
117b9b17debSTimur Tabi #define DMAW_DLY_CNT_BMSK                                      0xf0000
118b9b17debSTimur Tabi #define DMAW_DLY_CNT_SHFT                                           16
119b9b17debSTimur Tabi #define DMAR_DLY_CNT_BMSK                                       0xf800
120b9b17debSTimur Tabi #define DMAR_DLY_CNT_SHFT                                           11
121b9b17debSTimur Tabi #define DMAR_REQ_PRI                                             0x400
122b9b17debSTimur Tabi #define REGWRBLEN_BMSK                                           0x380
123b9b17debSTimur Tabi #define REGWRBLEN_SHFT                                               7
124b9b17debSTimur Tabi #define REGRDBLEN_BMSK                                            0x70
125b9b17debSTimur Tabi #define REGRDBLEN_SHFT                                               4
126b9b17debSTimur Tabi #define OUT_ORDER_MODE                                             0x4
127b9b17debSTimur Tabi #define ENH_ORDER_MODE                                             0x2
128b9b17debSTimur Tabi #define IN_ORDER_MODE                                              0x1
129b9b17debSTimur Tabi 
130b9b17debSTimur Tabi /* EMAC_MAILBOX_13 */
131b9b17debSTimur Tabi #define RFD3_PROC_IDX_BMSK                                   0xfff0000
132b9b17debSTimur Tabi #define RFD3_PROC_IDX_SHFT                                          16
133b9b17debSTimur Tabi #define RFD3_PROD_IDX_BMSK                                       0xfff
134b9b17debSTimur Tabi #define RFD3_PROD_IDX_SHFT                                           0
135b9b17debSTimur Tabi 
136b9b17debSTimur Tabi /* EMAC_MAILBOX_2 */
137b9b17debSTimur Tabi #define NTPD_CONS_IDX_BMSK                                  0xffff0000
138b9b17debSTimur Tabi #define NTPD_CONS_IDX_SHFT                                          16
139b9b17debSTimur Tabi 
140b9b17debSTimur Tabi /* EMAC_MAILBOX_3 */
141b9b17debSTimur Tabi #define RFD0_CONS_IDX_BMSK                                       0xfff
142b9b17debSTimur Tabi #define RFD0_CONS_IDX_SHFT                                           0
143b9b17debSTimur Tabi 
144b9b17debSTimur Tabi /* EMAC_MAILBOX_11 */
145b9b17debSTimur Tabi #define H3TPD_PROD_IDX_BMSK                                 0xffff0000
146b9b17debSTimur Tabi #define H3TPD_PROD_IDX_SHFT                                         16
147b9b17debSTimur Tabi 
148b9b17debSTimur Tabi /* EMAC_AXI_MAST_CTRL */
149b9b17debSTimur Tabi #define DATA_BYTE_SWAP                                             0x8
150b9b17debSTimur Tabi #define MAX_BOUND                                                  0x2
151b9b17debSTimur Tabi #define MAX_BTYPE                                                  0x1
152b9b17debSTimur Tabi 
153b9b17debSTimur Tabi /* EMAC_MAILBOX_12 */
154b9b17debSTimur Tabi #define H3TPD_CONS_IDX_BMSK                                 0xffff0000
155b9b17debSTimur Tabi #define H3TPD_CONS_IDX_SHFT                                         16
156b9b17debSTimur Tabi 
157b9b17debSTimur Tabi /* EMAC_MAILBOX_9 */
158b9b17debSTimur Tabi #define H2TPD_PROD_IDX_BMSK                                     0xffff
159b9b17debSTimur Tabi #define H2TPD_PROD_IDX_SHFT                                          0
160b9b17debSTimur Tabi 
161b9b17debSTimur Tabi /* EMAC_MAILBOX_10 */
162b9b17debSTimur Tabi #define H1TPD_CONS_IDX_BMSK                                 0xffff0000
163b9b17debSTimur Tabi #define H1TPD_CONS_IDX_SHFT                                         16
164b9b17debSTimur Tabi #define H2TPD_CONS_IDX_BMSK                                     0xffff
165b9b17debSTimur Tabi #define H2TPD_CONS_IDX_SHFT                                          0
166b9b17debSTimur Tabi 
167b9b17debSTimur Tabi /* EMAC_ATHR_HEADER_CTRL */
168b9b17debSTimur Tabi #define HEADER_CNT_EN                                              0x2
169b9b17debSTimur Tabi #define HEADER_ENABLE                                              0x1
170b9b17debSTimur Tabi 
171b9b17debSTimur Tabi /* EMAC_MAILBOX_0 */
172b9b17debSTimur Tabi #define RFD0_PROC_IDX_BMSK                                   0xfff0000
173b9b17debSTimur Tabi #define RFD0_PROC_IDX_SHFT                                          16
174b9b17debSTimur Tabi #define RFD0_PROD_IDX_BMSK                                       0xfff
175b9b17debSTimur Tabi #define RFD0_PROD_IDX_SHFT                                           0
176b9b17debSTimur Tabi 
177b9b17debSTimur Tabi /* EMAC_MAILBOX_5 */
178b9b17debSTimur Tabi #define RFD1_PROC_IDX_BMSK                                   0xfff0000
179b9b17debSTimur Tabi #define RFD1_PROC_IDX_SHFT                                          16
180b9b17debSTimur Tabi #define RFD1_PROD_IDX_BMSK                                       0xfff
181b9b17debSTimur Tabi #define RFD1_PROD_IDX_SHFT                                           0
182b9b17debSTimur Tabi 
183b9b17debSTimur Tabi /* EMAC_MISC_CTRL */
184b9b17debSTimur Tabi #define RX_UNCPL_INT_EN                                            0x1
185b9b17debSTimur Tabi 
186b9b17debSTimur Tabi /* EMAC_MAILBOX_7 */
187b9b17debSTimur Tabi #define RFD2_CONS_IDX_BMSK                                   0xfff0000
188b9b17debSTimur Tabi #define RFD2_CONS_IDX_SHFT                                          16
189b9b17debSTimur Tabi #define RFD1_CONS_IDX_BMSK                                       0xfff
190b9b17debSTimur Tabi #define RFD1_CONS_IDX_SHFT                                           0
191b9b17debSTimur Tabi 
192b9b17debSTimur Tabi /* EMAC_MAILBOX_8 */
193b9b17debSTimur Tabi #define RFD3_CONS_IDX_BMSK                                       0xfff
194b9b17debSTimur Tabi #define RFD3_CONS_IDX_SHFT                                           0
195b9b17debSTimur Tabi 
196b9b17debSTimur Tabi /* EMAC_MAILBOX_15 */
197b9b17debSTimur Tabi #define NTPD_PROD_IDX_BMSK                                      0xffff
198b9b17debSTimur Tabi #define NTPD_PROD_IDX_SHFT                                           0
199b9b17debSTimur Tabi 
200b9b17debSTimur Tabi /* EMAC_MAILBOX_16 */
201b9b17debSTimur Tabi #define H1TPD_PROD_IDX_BMSK                                     0xffff
202b9b17debSTimur Tabi #define H1TPD_PROD_IDX_SHFT                                          0
203b9b17debSTimur Tabi 
204b9b17debSTimur Tabi #define RXQ0_RSS_HSTYP_IPV6_TCP_EN                                0x20
205b9b17debSTimur Tabi #define RXQ0_RSS_HSTYP_IPV6_EN                                    0x10
206b9b17debSTimur Tabi #define RXQ0_RSS_HSTYP_IPV4_TCP_EN                                 0x8
207b9b17debSTimur Tabi #define RXQ0_RSS_HSTYP_IPV4_EN                                     0x4
208b9b17debSTimur Tabi 
209b9b17debSTimur Tabi /* EMAC_EMAC_WRAPPER_TX_TS_INX */
210b9b17debSTimur Tabi #define EMAC_WRAPPER_TX_TS_EMPTY                               BIT(31)
211b9b17debSTimur Tabi #define EMAC_WRAPPER_TX_TS_INX_BMSK                             0xffff
212b9b17debSTimur Tabi 
213b9b17debSTimur Tabi struct emac_skb_cb {
214b9b17debSTimur Tabi 	u32           tpd_idx;
215b9b17debSTimur Tabi 	unsigned long jiffies;
216b9b17debSTimur Tabi };
217b9b17debSTimur Tabi 
218b9b17debSTimur Tabi #define EMAC_SKB_CB(skb)	((struct emac_skb_cb *)(skb)->cb)
219b9b17debSTimur Tabi #define EMAC_RSS_IDT_SIZE	256
220b9b17debSTimur Tabi #define JUMBO_1KAH		0x4
221b9b17debSTimur Tabi #define RXD_TH			0x100
222b9b17debSTimur Tabi #define EMAC_TPD_LAST_FRAGMENT	0x80000000
223b9b17debSTimur Tabi #define EMAC_TPD_TSTAMP_SAVE	0x80000000
224b9b17debSTimur Tabi 
225b9b17debSTimur Tabi /* EMAC Errors in emac_rrd.word[3] */
226b9b17debSTimur Tabi #define EMAC_RRD_L4F		BIT(14)
227b9b17debSTimur Tabi #define EMAC_RRD_IPF		BIT(15)
228b9b17debSTimur Tabi #define EMAC_RRD_CRC		BIT(21)
229b9b17debSTimur Tabi #define EMAC_RRD_FAE		BIT(22)
230b9b17debSTimur Tabi #define EMAC_RRD_TRN		BIT(23)
231b9b17debSTimur Tabi #define EMAC_RRD_RNT		BIT(24)
232b9b17debSTimur Tabi #define EMAC_RRD_INC		BIT(25)
233b9b17debSTimur Tabi #define EMAC_RRD_FOV		BIT(29)
234b9b17debSTimur Tabi #define EMAC_RRD_LEN		BIT(30)
235b9b17debSTimur Tabi 
236b9b17debSTimur Tabi /* Error bits that will result in a received frame being discarded */
237b9b17debSTimur Tabi #define EMAC_RRD_ERROR (EMAC_RRD_IPF | EMAC_RRD_CRC | EMAC_RRD_FAE | \
238b9b17debSTimur Tabi 			EMAC_RRD_TRN | EMAC_RRD_RNT | EMAC_RRD_INC | \
239b9b17debSTimur Tabi 			EMAC_RRD_FOV | EMAC_RRD_LEN)
240b9b17debSTimur Tabi #define EMAC_RRD_STATS_DW_IDX 3
241b9b17debSTimur Tabi 
242b9b17debSTimur Tabi #define EMAC_RRD(RXQ, SIZE, IDX)	((RXQ)->rrd.v_addr + (SIZE * (IDX)))
243b9b17debSTimur Tabi #define EMAC_RFD(RXQ, SIZE, IDX)	((RXQ)->rfd.v_addr + (SIZE * (IDX)))
244b9b17debSTimur Tabi #define EMAC_TPD(TXQ, SIZE, IDX)	((TXQ)->tpd.v_addr + (SIZE * (IDX)))
245b9b17debSTimur Tabi 
246b9b17debSTimur Tabi #define GET_RFD_BUFFER(RXQ, IDX)	(&((RXQ)->rfd.rfbuff[(IDX)]))
247b9b17debSTimur Tabi #define GET_TPD_BUFFER(RTQ, IDX)	(&((RTQ)->tpd.tpbuff[(IDX)]))
248b9b17debSTimur Tabi 
249b9b17debSTimur Tabi #define EMAC_TX_POLL_HWTXTSTAMP_THRESHOLD	8
250b9b17debSTimur Tabi 
251b9b17debSTimur Tabi #define ISR_RX_PKT      (\
252b9b17debSTimur Tabi 	RX_PKT_INT0     |\
253b9b17debSTimur Tabi 	RX_PKT_INT1     |\
254b9b17debSTimur Tabi 	RX_PKT_INT2     |\
255b9b17debSTimur Tabi 	RX_PKT_INT3)
256b9b17debSTimur Tabi 
257b9b17debSTimur Tabi void emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr)
258b9b17debSTimur Tabi {
259b9b17debSTimur Tabi 	u32 crc32, bit, reg, mta;
260b9b17debSTimur Tabi 
261b9b17debSTimur Tabi 	/* Calculate the CRC of the MAC address */
262b9b17debSTimur Tabi 	crc32 = ether_crc(ETH_ALEN, addr);
263b9b17debSTimur Tabi 
264b9b17debSTimur Tabi 	/* The HASH Table is an array of 2 32-bit registers. It is
265b9b17debSTimur Tabi 	 * treated like an array of 64 bits (BitArray[hash_value]).
266b9b17debSTimur Tabi 	 * Use the upper 6 bits of the above CRC as the hash value.
267b9b17debSTimur Tabi 	 */
268b9b17debSTimur Tabi 	reg = (crc32 >> 31) & 0x1;
269b9b17debSTimur Tabi 	bit = (crc32 >> 26) & 0x1F;
270b9b17debSTimur Tabi 
271b9b17debSTimur Tabi 	mta = readl(adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
272b9b17debSTimur Tabi 	mta |= BIT(bit);
273b9b17debSTimur Tabi 	writel(mta, adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
274b9b17debSTimur Tabi }
275b9b17debSTimur Tabi 
276b9b17debSTimur Tabi void emac_mac_multicast_addr_clear(struct emac_adapter *adpt)
277b9b17debSTimur Tabi {
278b9b17debSTimur Tabi 	writel(0, adpt->base + EMAC_HASH_TAB_REG0);
279b9b17debSTimur Tabi 	writel(0, adpt->base + EMAC_HASH_TAB_REG1);
280b9b17debSTimur Tabi }
281b9b17debSTimur Tabi 
282b9b17debSTimur Tabi /* definitions for RSS */
283b9b17debSTimur Tabi #define EMAC_RSS_KEY(_i, _type) \
284b9b17debSTimur Tabi 		(EMAC_RSS_KEY0 + ((_i) * sizeof(_type)))
285b9b17debSTimur Tabi #define EMAC_RSS_TBL(_i, _type) \
286b9b17debSTimur Tabi 		(EMAC_IDT_TABLE0 + ((_i) * sizeof(_type)))
287b9b17debSTimur Tabi 
288b9b17debSTimur Tabi /* Config MAC modes */
289b9b17debSTimur Tabi void emac_mac_mode_config(struct emac_adapter *adpt)
290b9b17debSTimur Tabi {
291b9b17debSTimur Tabi 	struct net_device *netdev = adpt->netdev;
292b9b17debSTimur Tabi 	u32 mac;
293b9b17debSTimur Tabi 
294b9b17debSTimur Tabi 	mac = readl(adpt->base + EMAC_MAC_CTRL);
295b9b17debSTimur Tabi 	mac &= ~(VLAN_STRIP | PROM_MODE | MULTI_ALL | MAC_LP_EN);
296b9b17debSTimur Tabi 
297b9b17debSTimur Tabi 	if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
298b9b17debSTimur Tabi 		mac |= VLAN_STRIP;
299b9b17debSTimur Tabi 
300b9b17debSTimur Tabi 	if (netdev->flags & IFF_PROMISC)
301b9b17debSTimur Tabi 		mac |= PROM_MODE;
302b9b17debSTimur Tabi 
303b9b17debSTimur Tabi 	if (netdev->flags & IFF_ALLMULTI)
304b9b17debSTimur Tabi 		mac |= MULTI_ALL;
305b9b17debSTimur Tabi 
306b9b17debSTimur Tabi 	writel(mac, adpt->base + EMAC_MAC_CTRL);
307b9b17debSTimur Tabi }
308b9b17debSTimur Tabi 
309b9b17debSTimur Tabi /* Config descriptor rings */
310b9b17debSTimur Tabi static void emac_mac_dma_rings_config(struct emac_adapter *adpt)
311b9b17debSTimur Tabi {
312b9b17debSTimur Tabi 	static const unsigned short tpd_q_offset[] = {
313b9b17debSTimur Tabi 		EMAC_DESC_CTRL_8,        EMAC_H1TPD_BASE_ADDR_LO,
314b9b17debSTimur Tabi 		EMAC_H2TPD_BASE_ADDR_LO, EMAC_H3TPD_BASE_ADDR_LO};
315b9b17debSTimur Tabi 	static const unsigned short rfd_q_offset[] = {
316b9b17debSTimur Tabi 		EMAC_DESC_CTRL_2,        EMAC_DESC_CTRL_10,
317b9b17debSTimur Tabi 		EMAC_DESC_CTRL_12,       EMAC_DESC_CTRL_13};
318b9b17debSTimur Tabi 	static const unsigned short rrd_q_offset[] = {
319b9b17debSTimur Tabi 		EMAC_DESC_CTRL_5,        EMAC_DESC_CTRL_14,
320b9b17debSTimur Tabi 		EMAC_DESC_CTRL_15,       EMAC_DESC_CTRL_16};
321b9b17debSTimur Tabi 
322b9b17debSTimur Tabi 	/* TPD (Transmit Packet Descriptor) */
323b9b17debSTimur Tabi 	writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
324b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_1);
325b9b17debSTimur Tabi 
326b9b17debSTimur Tabi 	writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
327b9b17debSTimur Tabi 	       adpt->base + tpd_q_offset[0]);
328b9b17debSTimur Tabi 
329b9b17debSTimur Tabi 	writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
330b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_9);
331b9b17debSTimur Tabi 
332b9b17debSTimur Tabi 	/* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
333b9b17debSTimur Tabi 	writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
334b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_0);
335b9b17debSTimur Tabi 
336b9b17debSTimur Tabi 	writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
337b9b17debSTimur Tabi 	       adpt->base + rfd_q_offset[0]);
338b9b17debSTimur Tabi 	writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
339b9b17debSTimur Tabi 	       adpt->base + rrd_q_offset[0]);
340b9b17debSTimur Tabi 
341b9b17debSTimur Tabi 	writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
342b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_3);
343b9b17debSTimur Tabi 	writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
344b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_6);
345b9b17debSTimur Tabi 
346b9b17debSTimur Tabi 	writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
347b9b17debSTimur Tabi 	       adpt->base + EMAC_DESC_CTRL_4);
348b9b17debSTimur Tabi 
349b9b17debSTimur Tabi 	writel(0, adpt->base + EMAC_DESC_CTRL_11);
350b9b17debSTimur Tabi 
351b9b17debSTimur Tabi 	/* Load all of the base addresses above and ensure that triggering HW to
352b9b17debSTimur Tabi 	 * read ring pointers is flushed
353b9b17debSTimur Tabi 	 */
354b9b17debSTimur Tabi 	writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
355b9b17debSTimur Tabi }
356b9b17debSTimur Tabi 
357b9b17debSTimur Tabi /* Config transmit parameters */
358b9b17debSTimur Tabi static void emac_mac_tx_config(struct emac_adapter *adpt)
359b9b17debSTimur Tabi {
360b9b17debSTimur Tabi 	u32 val;
361b9b17debSTimur Tabi 
362b9b17debSTimur Tabi 	writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
363b9b17debSTimur Tabi 	       JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
364b9b17debSTimur Tabi 
365b9b17debSTimur Tabi 	val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
366b9b17debSTimur Tabi 	       NUM_TPD_BURST_PREF_BMSK;
367b9b17debSTimur Tabi 
368b9b17debSTimur Tabi 	val |= TXQ_MODE | LS_8023_SP;
369b9b17debSTimur Tabi 	val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
370b9b17debSTimur Tabi 		NUM_TXF_BURST_PREF_BMSK;
371b9b17debSTimur Tabi 
372b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_TXQ_CTRL_0);
373b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
374b9b17debSTimur Tabi 			  (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
375b9b17debSTimur Tabi }
376b9b17debSTimur Tabi 
377b9b17debSTimur Tabi /* Config receive parameters */
378b9b17debSTimur Tabi static void emac_mac_rx_config(struct emac_adapter *adpt)
379b9b17debSTimur Tabi {
380b9b17debSTimur Tabi 	u32 val;
381b9b17debSTimur Tabi 
382b9b17debSTimur Tabi 	val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
383b9b17debSTimur Tabi 	       NUM_RFD_BURST_PREF_BMSK;
384b9b17debSTimur Tabi 	val |= (SP_IPV6 | CUT_THRU_EN);
385b9b17debSTimur Tabi 
386b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_RXQ_CTRL_0);
387b9b17debSTimur Tabi 
388b9b17debSTimur Tabi 	val = readl(adpt->base + EMAC_RXQ_CTRL_1);
389b9b17debSTimur Tabi 	val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
390b9b17debSTimur Tabi 		 RFD_PREF_UP_THRESHOLD_BMSK);
391b9b17debSTimur Tabi 	val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
392b9b17debSTimur Tabi 		(RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
393b9b17debSTimur Tabi 		(RFD_PREF_UP_TH  << RFD_PREF_UP_THRESHOLD_SHFT);
394b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_RXQ_CTRL_1);
395b9b17debSTimur Tabi 
396b9b17debSTimur Tabi 	val = readl(adpt->base + EMAC_RXQ_CTRL_2);
397b9b17debSTimur Tabi 	val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
398b9b17debSTimur Tabi 	val |= (RXF_DOF_THRESFHOLD  << RXF_DOF_THRESHOLD_SHFT) |
399b9b17debSTimur Tabi 		(RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
400b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_RXQ_CTRL_2);
401b9b17debSTimur Tabi 
402b9b17debSTimur Tabi 	val = readl(adpt->base + EMAC_RXQ_CTRL_3);
403b9b17debSTimur Tabi 	val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
404b9b17debSTimur Tabi 	val |= RXD_TH << RXD_THRESHOLD_SHFT;
405b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_RXQ_CTRL_3);
406b9b17debSTimur Tabi }
407b9b17debSTimur Tabi 
408b9b17debSTimur Tabi /* Config dma */
409b9b17debSTimur Tabi static void emac_mac_dma_config(struct emac_adapter *adpt)
410b9b17debSTimur Tabi {
411b9b17debSTimur Tabi 	u32 dma_ctrl = DMAR_REQ_PRI;
412b9b17debSTimur Tabi 
413b9b17debSTimur Tabi 	switch (adpt->dma_order) {
414b9b17debSTimur Tabi 	case emac_dma_ord_in:
415b9b17debSTimur Tabi 		dma_ctrl |= IN_ORDER_MODE;
416b9b17debSTimur Tabi 		break;
417b9b17debSTimur Tabi 	case emac_dma_ord_enh:
418b9b17debSTimur Tabi 		dma_ctrl |= ENH_ORDER_MODE;
419b9b17debSTimur Tabi 		break;
420b9b17debSTimur Tabi 	case emac_dma_ord_out:
421b9b17debSTimur Tabi 		dma_ctrl |= OUT_ORDER_MODE;
422b9b17debSTimur Tabi 		break;
423b9b17debSTimur Tabi 	default:
424b9b17debSTimur Tabi 		break;
425b9b17debSTimur Tabi 	}
426b9b17debSTimur Tabi 
427b9b17debSTimur Tabi 	dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
428b9b17debSTimur Tabi 						REGRDBLEN_BMSK;
429b9b17debSTimur Tabi 	dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
430b9b17debSTimur Tabi 						REGWRBLEN_BMSK;
431b9b17debSTimur Tabi 	dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
432b9b17debSTimur Tabi 						DMAR_DLY_CNT_BMSK;
433b9b17debSTimur Tabi 	dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
434b9b17debSTimur Tabi 						DMAW_DLY_CNT_BMSK;
435b9b17debSTimur Tabi 
436b9b17debSTimur Tabi 	/* config DMA and ensure that configuration is flushed to HW */
437b9b17debSTimur Tabi 	writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
438b9b17debSTimur Tabi }
439b9b17debSTimur Tabi 
440b9b17debSTimur Tabi /* set MAC address */
441b9b17debSTimur Tabi static void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
442b9b17debSTimur Tabi {
443b9b17debSTimur Tabi 	u32 sta;
444b9b17debSTimur Tabi 
445b9b17debSTimur Tabi 	/* for example: 00-A0-C6-11-22-33
446b9b17debSTimur Tabi 	 * 0<-->C6112233, 1<-->00A0.
447b9b17debSTimur Tabi 	 */
448b9b17debSTimur Tabi 
449b9b17debSTimur Tabi 	/* low 32bit word */
450b9b17debSTimur Tabi 	sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
451b9b17debSTimur Tabi 	      (((u32)addr[4]) << 8)  | (((u32)addr[5]));
452b9b17debSTimur Tabi 	writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
453b9b17debSTimur Tabi 
454b9b17debSTimur Tabi 	/* hight 32bit word */
455b9b17debSTimur Tabi 	sta = (((u32)addr[0]) << 8) | (u32)addr[1];
456b9b17debSTimur Tabi 	writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
457b9b17debSTimur Tabi }
458b9b17debSTimur Tabi 
459b9b17debSTimur Tabi static void emac_mac_config(struct emac_adapter *adpt)
460b9b17debSTimur Tabi {
461b9b17debSTimur Tabi 	struct net_device *netdev = adpt->netdev;
462b9b17debSTimur Tabi 	unsigned int max_frame;
463b9b17debSTimur Tabi 	u32 val;
464b9b17debSTimur Tabi 
465b9b17debSTimur Tabi 	emac_set_mac_address(adpt, netdev->dev_addr);
466b9b17debSTimur Tabi 
467b9b17debSTimur Tabi 	max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
468b9b17debSTimur Tabi 	adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
469b9b17debSTimur Tabi 		ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
470b9b17debSTimur Tabi 
471b9b17debSTimur Tabi 	emac_mac_dma_rings_config(adpt);
472b9b17debSTimur Tabi 
473b9b17debSTimur Tabi 	writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
474b9b17debSTimur Tabi 	       adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
475b9b17debSTimur Tabi 
476b9b17debSTimur Tabi 	emac_mac_tx_config(adpt);
477b9b17debSTimur Tabi 	emac_mac_rx_config(adpt);
478b9b17debSTimur Tabi 	emac_mac_dma_config(adpt);
479b9b17debSTimur Tabi 
480b9b17debSTimur Tabi 	val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
481b9b17debSTimur Tabi 	val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
482b9b17debSTimur Tabi 	val |= MAX_BTYPE;
483b9b17debSTimur Tabi 	writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
484b9b17debSTimur Tabi 	writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
485b9b17debSTimur Tabi 	writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
486b9b17debSTimur Tabi }
487b9b17debSTimur Tabi 
488b9b17debSTimur Tabi void emac_mac_reset(struct emac_adapter *adpt)
489b9b17debSTimur Tabi {
490b9b17debSTimur Tabi 	emac_mac_stop(adpt);
491b9b17debSTimur Tabi 
492b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
493b9b17debSTimur Tabi 	usleep_range(100, 150); /* reset may take up to 100usec */
494b9b17debSTimur Tabi 
495b9b17debSTimur Tabi 	/* interrupt clear-on-read */
496b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
497b9b17debSTimur Tabi }
498b9b17debSTimur Tabi 
4990f20276dSTimur Tabi static void emac_mac_start(struct emac_adapter *adpt)
500b9b17debSTimur Tabi {
501b9b17debSTimur Tabi 	struct phy_device *phydev = adpt->phydev;
502b9b17debSTimur Tabi 	u32 mac, csr1;
503b9b17debSTimur Tabi 
504b9b17debSTimur Tabi 	/* enable tx queue */
505b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
506b9b17debSTimur Tabi 
507b9b17debSTimur Tabi 	/* enable rx queue */
508b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
509b9b17debSTimur Tabi 
510b9b17debSTimur Tabi 	/* enable mac control */
511b9b17debSTimur Tabi 	mac = readl(adpt->base + EMAC_MAC_CTRL);
512b9b17debSTimur Tabi 	csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
513b9b17debSTimur Tabi 
514b9b17debSTimur Tabi 	mac |= TXEN | RXEN;     /* enable RX/TX */
515b9b17debSTimur Tabi 
516b44700e9STimur Tabi 	/* Configure MAC flow control. If set to automatic, then match
517b44700e9STimur Tabi 	 * whatever the PHY does. Otherwise, enable or disable it, depending
518b44700e9STimur Tabi 	 * on what the user configured via ethtool.
519b44700e9STimur Tabi 	 */
520b44700e9STimur Tabi 	mac &= ~(RXFC | TXFC);
521b44700e9STimur Tabi 
522b44700e9STimur Tabi 	if (adpt->automatic) {
523b44700e9STimur Tabi 		/* If it's set to automatic, then update our local values */
524b44700e9STimur Tabi 		adpt->rx_flow_control = phydev->pause;
525b44700e9STimur Tabi 		adpt->tx_flow_control = phydev->pause != phydev->asym_pause;
526b44700e9STimur Tabi 	}
527b44700e9STimur Tabi 	mac |= adpt->rx_flow_control ? RXFC : 0;
528b44700e9STimur Tabi 	mac |= adpt->tx_flow_control ? TXFC : 0;
529b9b17debSTimur Tabi 
530b9b17debSTimur Tabi 	/* setup link speed */
531b9b17debSTimur Tabi 	mac &= ~SPEED_MASK;
532b9b17debSTimur Tabi 	if (phydev->speed == SPEED_1000) {
533b9b17debSTimur Tabi 		mac |= SPEED(2);
534b9b17debSTimur Tabi 		csr1 |= FREQ_MODE;
535b9b17debSTimur Tabi 	} else {
536b9b17debSTimur Tabi 		mac |= SPEED(1);
537b9b17debSTimur Tabi 		csr1 &= ~FREQ_MODE;
538b9b17debSTimur Tabi 	}
539b9b17debSTimur Tabi 
540b9b17debSTimur Tabi 	if (phydev->duplex == DUPLEX_FULL)
541b9b17debSTimur Tabi 		mac |= FULLD;
542b9b17debSTimur Tabi 	else
543b9b17debSTimur Tabi 		mac &= ~FULLD;
544b9b17debSTimur Tabi 
545b9b17debSTimur Tabi 	/* other parameters */
546b9b17debSTimur Tabi 	mac |= (CRCE | PCRCE);
547b9b17debSTimur Tabi 	mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
548b9b17debSTimur Tabi 	mac |= BROAD_EN;
549b9b17debSTimur Tabi 	mac |= FLCHK;
550b9b17debSTimur Tabi 	mac &= ~RX_CHKSUM_EN;
551b9b17debSTimur Tabi 	mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
552b9b17debSTimur Tabi 		 DEBUG_MODE | SINGLE_PAUSE_MODE);
553b9b17debSTimur Tabi 
554*4a7a3860STimur Tabi 	/* Enable single-pause-frame mode if requested.
555*4a7a3860STimur Tabi 	 *
556*4a7a3860STimur Tabi 	 * If enabled, the EMAC will send a single pause frame when the RX
557*4a7a3860STimur Tabi 	 * queue is full.  This normally leads to packet loss because
558*4a7a3860STimur Tabi 	 * the pause frame disables the remote MAC only for 33ms (the quanta),
559*4a7a3860STimur Tabi 	 * and then the remote MAC continues sending packets even though
560*4a7a3860STimur Tabi 	 * the RX queue is still full.
561*4a7a3860STimur Tabi 	 *
562*4a7a3860STimur Tabi 	 * If disabled, the EMAC sends a pause frame every 31ms until the RX
563*4a7a3860STimur Tabi 	 * queue is no longer full.  Normally, this is the preferred
564*4a7a3860STimur Tabi 	 * method of operation.  However, when the system is hung (e.g.
565*4a7a3860STimur Tabi 	 * cores are halted), the EMAC interrupt handler is never called
566*4a7a3860STimur Tabi 	 * and so the RX queue fills up quickly and stays full.  The resuling
567*4a7a3860STimur Tabi 	 * non-stop "flood" of pause frames sometimes has the effect of
568*4a7a3860STimur Tabi 	 * disabling nearby switches.  In some cases, other nearby switches
569*4a7a3860STimur Tabi 	 * are also affected, shutting down the entire network.
570*4a7a3860STimur Tabi 	 *
571*4a7a3860STimur Tabi 	 * The user can enable or disable single-pause-frame mode
572*4a7a3860STimur Tabi 	 * via ethtool.
573*4a7a3860STimur Tabi 	 */
574*4a7a3860STimur Tabi 	mac |= adpt->single_pause_mode ? SINGLE_PAUSE_MODE : 0;
575*4a7a3860STimur Tabi 
576b9b17debSTimur Tabi 	writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
577b9b17debSTimur Tabi 
578b9b17debSTimur Tabi 	writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
579b9b17debSTimur Tabi 
580b9b17debSTimur Tabi 	/* enable interrupt read clear, low power sleep mode and
581b9b17debSTimur Tabi 	 * the irq moderators
582b9b17debSTimur Tabi 	 */
583b9b17debSTimur Tabi 
584b9b17debSTimur Tabi 	writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
585b9b17debSTimur Tabi 	writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
586b9b17debSTimur Tabi 			IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
587b9b17debSTimur Tabi 
588b9b17debSTimur Tabi 	emac_mac_mode_config(adpt);
589b9b17debSTimur Tabi 
590b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
591b9b17debSTimur Tabi 			  (HEADER_ENABLE | HEADER_CNT_EN), 0);
592b9b17debSTimur Tabi }
593b9b17debSTimur Tabi 
594b9b17debSTimur Tabi void emac_mac_stop(struct emac_adapter *adpt)
595b9b17debSTimur Tabi {
596b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
597b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
598b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
599b9b17debSTimur Tabi 	usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
600b9b17debSTimur Tabi }
601b9b17debSTimur Tabi 
602b9b17debSTimur Tabi /* Free all descriptors of given transmit queue */
603b9b17debSTimur Tabi static void emac_tx_q_descs_free(struct emac_adapter *adpt)
604b9b17debSTimur Tabi {
605b9b17debSTimur Tabi 	struct emac_tx_queue *tx_q = &adpt->tx_q;
606b9b17debSTimur Tabi 	unsigned int i;
607b9b17debSTimur Tabi 	size_t size;
608b9b17debSTimur Tabi 
609b9b17debSTimur Tabi 	/* ring already cleared, nothing to do */
610b9b17debSTimur Tabi 	if (!tx_q->tpd.tpbuff)
611b9b17debSTimur Tabi 		return;
612b9b17debSTimur Tabi 
613b9b17debSTimur Tabi 	for (i = 0; i < tx_q->tpd.count; i++) {
614b9b17debSTimur Tabi 		struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
615b9b17debSTimur Tabi 
616b9b17debSTimur Tabi 		if (tpbuf->dma_addr) {
617b9b17debSTimur Tabi 			dma_unmap_single(adpt->netdev->dev.parent,
618b9b17debSTimur Tabi 					 tpbuf->dma_addr, tpbuf->length,
619b9b17debSTimur Tabi 					 DMA_TO_DEVICE);
620b9b17debSTimur Tabi 			tpbuf->dma_addr = 0;
621b9b17debSTimur Tabi 		}
622b9b17debSTimur Tabi 		if (tpbuf->skb) {
623b9b17debSTimur Tabi 			dev_kfree_skb_any(tpbuf->skb);
624b9b17debSTimur Tabi 			tpbuf->skb = NULL;
625b9b17debSTimur Tabi 		}
626b9b17debSTimur Tabi 	}
627b9b17debSTimur Tabi 
628b9b17debSTimur Tabi 	size = sizeof(struct emac_buffer) * tx_q->tpd.count;
629b9b17debSTimur Tabi 	memset(tx_q->tpd.tpbuff, 0, size);
630b9b17debSTimur Tabi 
631b9b17debSTimur Tabi 	/* clear the descriptor ring */
632b9b17debSTimur Tabi 	memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
633b9b17debSTimur Tabi 
634b9b17debSTimur Tabi 	tx_q->tpd.consume_idx = 0;
635b9b17debSTimur Tabi 	tx_q->tpd.produce_idx = 0;
636b9b17debSTimur Tabi }
637b9b17debSTimur Tabi 
638b9b17debSTimur Tabi /* Free all descriptors of given receive queue */
639b9b17debSTimur Tabi static void emac_rx_q_free_descs(struct emac_adapter *adpt)
640b9b17debSTimur Tabi {
641b9b17debSTimur Tabi 	struct device *dev = adpt->netdev->dev.parent;
642b9b17debSTimur Tabi 	struct emac_rx_queue *rx_q = &adpt->rx_q;
643b9b17debSTimur Tabi 	unsigned int i;
644b9b17debSTimur Tabi 	size_t size;
645b9b17debSTimur Tabi 
646b9b17debSTimur Tabi 	/* ring already cleared, nothing to do */
647b9b17debSTimur Tabi 	if (!rx_q->rfd.rfbuff)
648b9b17debSTimur Tabi 		return;
649b9b17debSTimur Tabi 
650b9b17debSTimur Tabi 	for (i = 0; i < rx_q->rfd.count; i++) {
651b9b17debSTimur Tabi 		struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
652b9b17debSTimur Tabi 
653b9b17debSTimur Tabi 		if (rfbuf->dma_addr) {
654b9b17debSTimur Tabi 			dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
655b9b17debSTimur Tabi 					 DMA_FROM_DEVICE);
656b9b17debSTimur Tabi 			rfbuf->dma_addr = 0;
657b9b17debSTimur Tabi 		}
658b9b17debSTimur Tabi 		if (rfbuf->skb) {
659b9b17debSTimur Tabi 			dev_kfree_skb(rfbuf->skb);
660b9b17debSTimur Tabi 			rfbuf->skb = NULL;
661b9b17debSTimur Tabi 		}
662b9b17debSTimur Tabi 	}
663b9b17debSTimur Tabi 
664b9b17debSTimur Tabi 	size =  sizeof(struct emac_buffer) * rx_q->rfd.count;
665b9b17debSTimur Tabi 	memset(rx_q->rfd.rfbuff, 0, size);
666b9b17debSTimur Tabi 
667b9b17debSTimur Tabi 	/* clear the descriptor rings */
668b9b17debSTimur Tabi 	memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
669b9b17debSTimur Tabi 	rx_q->rrd.produce_idx = 0;
670b9b17debSTimur Tabi 	rx_q->rrd.consume_idx = 0;
671b9b17debSTimur Tabi 
672b9b17debSTimur Tabi 	memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
673b9b17debSTimur Tabi 	rx_q->rfd.produce_idx = 0;
674b9b17debSTimur Tabi 	rx_q->rfd.consume_idx = 0;
675b9b17debSTimur Tabi }
676b9b17debSTimur Tabi 
677b9b17debSTimur Tabi /* Free all buffers associated with given transmit queue */
678b9b17debSTimur Tabi static void emac_tx_q_bufs_free(struct emac_adapter *adpt)
679b9b17debSTimur Tabi {
680b9b17debSTimur Tabi 	struct emac_tx_queue *tx_q = &adpt->tx_q;
681b9b17debSTimur Tabi 
682b9b17debSTimur Tabi 	emac_tx_q_descs_free(adpt);
683b9b17debSTimur Tabi 
684b9b17debSTimur Tabi 	kfree(tx_q->tpd.tpbuff);
685b9b17debSTimur Tabi 	tx_q->tpd.tpbuff = NULL;
686b9b17debSTimur Tabi 	tx_q->tpd.v_addr = NULL;
687b9b17debSTimur Tabi 	tx_q->tpd.dma_addr = 0;
688b9b17debSTimur Tabi 	tx_q->tpd.size = 0;
689b9b17debSTimur Tabi }
690b9b17debSTimur Tabi 
691b9b17debSTimur Tabi /* Allocate TX descriptor ring for the given transmit queue */
692b9b17debSTimur Tabi static int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
693b9b17debSTimur Tabi 				struct emac_tx_queue *tx_q)
694b9b17debSTimur Tabi {
695b9b17debSTimur Tabi 	struct emac_ring_header *ring_header = &adpt->ring_header;
696b9b17debSTimur Tabi 	size_t size;
697b9b17debSTimur Tabi 
698b9b17debSTimur Tabi 	size = sizeof(struct emac_buffer) * tx_q->tpd.count;
699b9b17debSTimur Tabi 	tx_q->tpd.tpbuff = kzalloc(size, GFP_KERNEL);
700b9b17debSTimur Tabi 	if (!tx_q->tpd.tpbuff)
701b9b17debSTimur Tabi 		return -ENOMEM;
702b9b17debSTimur Tabi 
703b9b17debSTimur Tabi 	tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
704b9b17debSTimur Tabi 	tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
705b9b17debSTimur Tabi 	tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
706b9b17debSTimur Tabi 	ring_header->used += ALIGN(tx_q->tpd.size, 8);
707b9b17debSTimur Tabi 	tx_q->tpd.produce_idx = 0;
708b9b17debSTimur Tabi 	tx_q->tpd.consume_idx = 0;
709b9b17debSTimur Tabi 
710b9b17debSTimur Tabi 	return 0;
711b9b17debSTimur Tabi }
712b9b17debSTimur Tabi 
713b9b17debSTimur Tabi /* Free all buffers associated with given transmit queue */
714b9b17debSTimur Tabi static void emac_rx_q_bufs_free(struct emac_adapter *adpt)
715b9b17debSTimur Tabi {
716b9b17debSTimur Tabi 	struct emac_rx_queue *rx_q = &adpt->rx_q;
717b9b17debSTimur Tabi 
718b9b17debSTimur Tabi 	emac_rx_q_free_descs(adpt);
719b9b17debSTimur Tabi 
720b9b17debSTimur Tabi 	kfree(rx_q->rfd.rfbuff);
721b9b17debSTimur Tabi 	rx_q->rfd.rfbuff   = NULL;
722b9b17debSTimur Tabi 
723b9b17debSTimur Tabi 	rx_q->rfd.v_addr   = NULL;
724b9b17debSTimur Tabi 	rx_q->rfd.dma_addr = 0;
725b9b17debSTimur Tabi 	rx_q->rfd.size     = 0;
726b9b17debSTimur Tabi 
727b9b17debSTimur Tabi 	rx_q->rrd.v_addr   = NULL;
728b9b17debSTimur Tabi 	rx_q->rrd.dma_addr = 0;
729b9b17debSTimur Tabi 	rx_q->rrd.size     = 0;
730b9b17debSTimur Tabi }
731b9b17debSTimur Tabi 
732b9b17debSTimur Tabi /* Allocate RX descriptor rings for the given receive queue */
733b9b17debSTimur Tabi static int emac_rx_descs_alloc(struct emac_adapter *adpt)
734b9b17debSTimur Tabi {
735b9b17debSTimur Tabi 	struct emac_ring_header *ring_header = &adpt->ring_header;
736b9b17debSTimur Tabi 	struct emac_rx_queue *rx_q = &adpt->rx_q;
737b9b17debSTimur Tabi 	size_t size;
738b9b17debSTimur Tabi 
739b9b17debSTimur Tabi 	size = sizeof(struct emac_buffer) * rx_q->rfd.count;
740b9b17debSTimur Tabi 	rx_q->rfd.rfbuff = kzalloc(size, GFP_KERNEL);
741b9b17debSTimur Tabi 	if (!rx_q->rfd.rfbuff)
742b9b17debSTimur Tabi 		return -ENOMEM;
743b9b17debSTimur Tabi 
744b9b17debSTimur Tabi 	rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
745b9b17debSTimur Tabi 	rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
746b9b17debSTimur Tabi 
747b9b17debSTimur Tabi 	rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
748b9b17debSTimur Tabi 	rx_q->rrd.v_addr   = ring_header->v_addr + ring_header->used;
749b9b17debSTimur Tabi 	ring_header->used += ALIGN(rx_q->rrd.size, 8);
750b9b17debSTimur Tabi 
751b9b17debSTimur Tabi 	rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
752b9b17debSTimur Tabi 	rx_q->rfd.v_addr   = ring_header->v_addr + ring_header->used;
753b9b17debSTimur Tabi 	ring_header->used += ALIGN(rx_q->rfd.size, 8);
754b9b17debSTimur Tabi 
755b9b17debSTimur Tabi 	rx_q->rrd.produce_idx = 0;
756b9b17debSTimur Tabi 	rx_q->rrd.consume_idx = 0;
757b9b17debSTimur Tabi 
758b9b17debSTimur Tabi 	rx_q->rfd.produce_idx = 0;
759b9b17debSTimur Tabi 	rx_q->rfd.consume_idx = 0;
760b9b17debSTimur Tabi 
761b9b17debSTimur Tabi 	return 0;
762b9b17debSTimur Tabi }
763b9b17debSTimur Tabi 
764b9b17debSTimur Tabi /* Allocate all TX and RX descriptor rings */
765b9b17debSTimur Tabi int emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
766b9b17debSTimur Tabi {
767b9b17debSTimur Tabi 	struct emac_ring_header *ring_header = &adpt->ring_header;
768b9b17debSTimur Tabi 	struct device *dev = adpt->netdev->dev.parent;
769b9b17debSTimur Tabi 	unsigned int num_tx_descs = adpt->tx_desc_cnt;
770b9b17debSTimur Tabi 	unsigned int num_rx_descs = adpt->rx_desc_cnt;
771b9b17debSTimur Tabi 	int ret;
772b9b17debSTimur Tabi 
773b9b17debSTimur Tabi 	adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
774b9b17debSTimur Tabi 
775b9b17debSTimur Tabi 	adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
776b9b17debSTimur Tabi 	adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
777b9b17debSTimur Tabi 
778b9b17debSTimur Tabi 	/* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
779b9b17debSTimur Tabi 	 * hence the additional padding bytes are allocated.
780b9b17debSTimur Tabi 	 */
781b9b17debSTimur Tabi 	ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
782b9b17debSTimur Tabi 			    num_rx_descs * (adpt->rfd_size * 4) +
783b9b17debSTimur Tabi 			    num_rx_descs * (adpt->rrd_size * 4) +
784b9b17debSTimur Tabi 			    8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
785b9b17debSTimur Tabi 
786b9b17debSTimur Tabi 	ring_header->used = 0;
787b9b17debSTimur Tabi 	ring_header->v_addr = dma_zalloc_coherent(dev, ring_header->size,
788b9b17debSTimur Tabi 						 &ring_header->dma_addr,
789b9b17debSTimur Tabi 						 GFP_KERNEL);
790b9b17debSTimur Tabi 	if (!ring_header->v_addr)
791b9b17debSTimur Tabi 		return -ENOMEM;
792b9b17debSTimur Tabi 
793b9b17debSTimur Tabi 	ring_header->used = ALIGN(ring_header->dma_addr, 8) -
794b9b17debSTimur Tabi 							ring_header->dma_addr;
795b9b17debSTimur Tabi 
796b9b17debSTimur Tabi 	ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
797b9b17debSTimur Tabi 	if (ret) {
798b9b17debSTimur Tabi 		netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
799b9b17debSTimur Tabi 		goto err_alloc_tx;
800b9b17debSTimur Tabi 	}
801b9b17debSTimur Tabi 
802b9b17debSTimur Tabi 	ret = emac_rx_descs_alloc(adpt);
803b9b17debSTimur Tabi 	if (ret) {
804b9b17debSTimur Tabi 		netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
805b9b17debSTimur Tabi 		goto err_alloc_rx;
806b9b17debSTimur Tabi 	}
807b9b17debSTimur Tabi 
808b9b17debSTimur Tabi 	return 0;
809b9b17debSTimur Tabi 
810b9b17debSTimur Tabi err_alloc_rx:
811b9b17debSTimur Tabi 	emac_tx_q_bufs_free(adpt);
812b9b17debSTimur Tabi err_alloc_tx:
813b9b17debSTimur Tabi 	dma_free_coherent(dev, ring_header->size,
814b9b17debSTimur Tabi 			  ring_header->v_addr, ring_header->dma_addr);
815b9b17debSTimur Tabi 
816b9b17debSTimur Tabi 	ring_header->v_addr   = NULL;
817b9b17debSTimur Tabi 	ring_header->dma_addr = 0;
818b9b17debSTimur Tabi 	ring_header->size     = 0;
819b9b17debSTimur Tabi 	ring_header->used     = 0;
820b9b17debSTimur Tabi 
821b9b17debSTimur Tabi 	return ret;
822b9b17debSTimur Tabi }
823b9b17debSTimur Tabi 
824b9b17debSTimur Tabi /* Free all TX and RX descriptor rings */
825b9b17debSTimur Tabi void emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
826b9b17debSTimur Tabi {
827b9b17debSTimur Tabi 	struct emac_ring_header *ring_header = &adpt->ring_header;
828b9b17debSTimur Tabi 	struct device *dev = adpt->netdev->dev.parent;
829b9b17debSTimur Tabi 
830b9b17debSTimur Tabi 	emac_tx_q_bufs_free(adpt);
831b9b17debSTimur Tabi 	emac_rx_q_bufs_free(adpt);
832b9b17debSTimur Tabi 
833b9b17debSTimur Tabi 	dma_free_coherent(dev, ring_header->size,
834b9b17debSTimur Tabi 			  ring_header->v_addr, ring_header->dma_addr);
835b9b17debSTimur Tabi 
836b9b17debSTimur Tabi 	ring_header->v_addr   = NULL;
837b9b17debSTimur Tabi 	ring_header->dma_addr = 0;
838b9b17debSTimur Tabi 	ring_header->size     = 0;
839b9b17debSTimur Tabi 	ring_header->used     = 0;
840b9b17debSTimur Tabi }
841b9b17debSTimur Tabi 
842b9b17debSTimur Tabi /* Initialize descriptor rings */
843b9b17debSTimur Tabi static void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
844b9b17debSTimur Tabi {
845b9b17debSTimur Tabi 	unsigned int i;
846b9b17debSTimur Tabi 
847b9b17debSTimur Tabi 	adpt->tx_q.tpd.produce_idx = 0;
848b9b17debSTimur Tabi 	adpt->tx_q.tpd.consume_idx = 0;
849b9b17debSTimur Tabi 	for (i = 0; i < adpt->tx_q.tpd.count; i++)
850b9b17debSTimur Tabi 		adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
851b9b17debSTimur Tabi 
852b9b17debSTimur Tabi 	adpt->rx_q.rrd.produce_idx = 0;
853b9b17debSTimur Tabi 	adpt->rx_q.rrd.consume_idx = 0;
854b9b17debSTimur Tabi 	adpt->rx_q.rfd.produce_idx = 0;
855b9b17debSTimur Tabi 	adpt->rx_q.rfd.consume_idx = 0;
856b9b17debSTimur Tabi 	for (i = 0; i < adpt->rx_q.rfd.count; i++)
857b9b17debSTimur Tabi 		adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
858b9b17debSTimur Tabi }
859b9b17debSTimur Tabi 
860b9b17debSTimur Tabi /* Produce new receive free descriptor */
861b9b17debSTimur Tabi static void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
862b9b17debSTimur Tabi 				   struct emac_rx_queue *rx_q,
863b9b17debSTimur Tabi 				   dma_addr_t addr)
864b9b17debSTimur Tabi {
865b9b17debSTimur Tabi 	u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
866b9b17debSTimur Tabi 
867b9b17debSTimur Tabi 	*(hw_rfd++) = lower_32_bits(addr);
868b9b17debSTimur Tabi 	*hw_rfd = upper_32_bits(addr);
869b9b17debSTimur Tabi 
870b9b17debSTimur Tabi 	if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
871b9b17debSTimur Tabi 		rx_q->rfd.produce_idx = 0;
872b9b17debSTimur Tabi }
873b9b17debSTimur Tabi 
874b9b17debSTimur Tabi /* Fill up receive queue's RFD with preallocated receive buffers */
875b9b17debSTimur Tabi static void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
876b9b17debSTimur Tabi 				    struct emac_rx_queue *rx_q)
877b9b17debSTimur Tabi {
878b9b17debSTimur Tabi 	struct emac_buffer *curr_rxbuf;
879b9b17debSTimur Tabi 	struct emac_buffer *next_rxbuf;
880b9b17debSTimur Tabi 	unsigned int count = 0;
881b9b17debSTimur Tabi 	u32 next_produce_idx;
882b9b17debSTimur Tabi 
883b9b17debSTimur Tabi 	next_produce_idx = rx_q->rfd.produce_idx + 1;
884b9b17debSTimur Tabi 	if (next_produce_idx == rx_q->rfd.count)
885b9b17debSTimur Tabi 		next_produce_idx = 0;
886b9b17debSTimur Tabi 
887b9b17debSTimur Tabi 	curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
888b9b17debSTimur Tabi 	next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
889b9b17debSTimur Tabi 
890b9b17debSTimur Tabi 	/* this always has a blank rx_buffer*/
891b9b17debSTimur Tabi 	while (!next_rxbuf->dma_addr) {
892b9b17debSTimur Tabi 		struct sk_buff *skb;
893b9b17debSTimur Tabi 		int ret;
894b9b17debSTimur Tabi 
895b9b17debSTimur Tabi 		skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
896b9b17debSTimur Tabi 		if (!skb)
897b9b17debSTimur Tabi 			break;
898b9b17debSTimur Tabi 
899b9b17debSTimur Tabi 		curr_rxbuf->dma_addr =
900b9b17debSTimur Tabi 			dma_map_single(adpt->netdev->dev.parent, skb->data,
901b9b17debSTimur Tabi 				       curr_rxbuf->length, DMA_FROM_DEVICE);
902b9b17debSTimur Tabi 		ret = dma_mapping_error(adpt->netdev->dev.parent,
903b9b17debSTimur Tabi 					curr_rxbuf->dma_addr);
904b9b17debSTimur Tabi 		if (ret) {
905b9b17debSTimur Tabi 			dev_kfree_skb(skb);
906b9b17debSTimur Tabi 			break;
907b9b17debSTimur Tabi 		}
908b9b17debSTimur Tabi 		curr_rxbuf->skb = skb;
909b9b17debSTimur Tabi 		curr_rxbuf->length = adpt->rxbuf_size;
910b9b17debSTimur Tabi 
911b9b17debSTimur Tabi 		emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
912b9b17debSTimur Tabi 		next_produce_idx = rx_q->rfd.produce_idx + 1;
913b9b17debSTimur Tabi 		if (next_produce_idx == rx_q->rfd.count)
914b9b17debSTimur Tabi 			next_produce_idx = 0;
915b9b17debSTimur Tabi 
916b9b17debSTimur Tabi 		curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
917b9b17debSTimur Tabi 		next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
918b9b17debSTimur Tabi 		count++;
919b9b17debSTimur Tabi 	}
920b9b17debSTimur Tabi 
921b9b17debSTimur Tabi 	if (count) {
922b9b17debSTimur Tabi 		u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
923b9b17debSTimur Tabi 				rx_q->produce_mask;
924b9b17debSTimur Tabi 		emac_reg_update32(adpt->base + rx_q->produce_reg,
925b9b17debSTimur Tabi 				  rx_q->produce_mask, prod_idx);
926b9b17debSTimur Tabi 	}
927b9b17debSTimur Tabi }
928b9b17debSTimur Tabi 
929b9b17debSTimur Tabi static void emac_adjust_link(struct net_device *netdev)
930b9b17debSTimur Tabi {
931b9b17debSTimur Tabi 	struct emac_adapter *adpt = netdev_priv(netdev);
932fd0e97b8STimur Tabi 	struct emac_sgmii *sgmii = &adpt->phy;
933b9b17debSTimur Tabi 	struct phy_device *phydev = netdev->phydev;
934b9b17debSTimur Tabi 
935fd0e97b8STimur Tabi 	if (phydev->link) {
936b9b17debSTimur Tabi 		emac_mac_start(adpt);
937fd0e97b8STimur Tabi 		sgmii->link_up(adpt);
938fd0e97b8STimur Tabi 	} else {
939fd0e97b8STimur Tabi 		sgmii->link_down(adpt);
940b9b17debSTimur Tabi 		emac_mac_stop(adpt);
941fd0e97b8STimur Tabi 	}
942b9b17debSTimur Tabi 
943b9b17debSTimur Tabi 	phy_print_status(phydev);
944b9b17debSTimur Tabi }
945b9b17debSTimur Tabi 
946b9b17debSTimur Tabi /* Bringup the interface/HW */
947b9b17debSTimur Tabi int emac_mac_up(struct emac_adapter *adpt)
948b9b17debSTimur Tabi {
949b9b17debSTimur Tabi 	struct net_device *netdev = adpt->netdev;
950b9b17debSTimur Tabi 	int ret;
951b9b17debSTimur Tabi 
952b9b17debSTimur Tabi 	emac_mac_rx_tx_ring_reset_all(adpt);
953b9b17debSTimur Tabi 	emac_mac_config(adpt);
954b9b17debSTimur Tabi 	emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
955b9b17debSTimur Tabi 
95624609669STimur Tabi 	adpt->phydev->irq = PHY_POLL;
957b9b17debSTimur Tabi 	ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
958b9b17debSTimur Tabi 				 PHY_INTERFACE_MODE_SGMII);
959b9b17debSTimur Tabi 	if (ret) {
960b9b17debSTimur Tabi 		netdev_err(adpt->netdev, "could not connect phy\n");
961b9b17debSTimur Tabi 		return ret;
962b9b17debSTimur Tabi 	}
963b9b17debSTimur Tabi 
9649da34f27STimur Tabi 	phy_attached_print(adpt->phydev, NULL);
9659da34f27STimur Tabi 
966b9b17debSTimur Tabi 	/* enable mac irq */
967b9b17debSTimur Tabi 	writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
968b9b17debSTimur Tabi 	writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
969b9b17debSTimur Tabi 
970b9b17debSTimur Tabi 	phy_start(adpt->phydev);
971b9b17debSTimur Tabi 
972b9b17debSTimur Tabi 	napi_enable(&adpt->rx_q.napi);
973b9b17debSTimur Tabi 	netif_start_queue(netdev);
974b9b17debSTimur Tabi 
975b9b17debSTimur Tabi 	return 0;
976b9b17debSTimur Tabi }
977b9b17debSTimur Tabi 
978b9b17debSTimur Tabi /* Bring down the interface/HW */
979b9b17debSTimur Tabi void emac_mac_down(struct emac_adapter *adpt)
980b9b17debSTimur Tabi {
981b9b17debSTimur Tabi 	struct net_device *netdev = adpt->netdev;
982b9b17debSTimur Tabi 
983b9b17debSTimur Tabi 	netif_stop_queue(netdev);
984b9b17debSTimur Tabi 	napi_disable(&adpt->rx_q.napi);
985b9b17debSTimur Tabi 
986b9b17debSTimur Tabi 	phy_stop(adpt->phydev);
987b9b17debSTimur Tabi 
98893966b71STimur Tabi 	/* Interrupts must be disabled before the PHY is disconnected, to
98993966b71STimur Tabi 	 * avoid a race condition where adjust_link is null when we get
99093966b71STimur Tabi 	 * an interrupt.
99193966b71STimur Tabi 	 */
992b9b17debSTimur Tabi 	writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
993b9b17debSTimur Tabi 	writel(0, adpt->base + EMAC_INT_MASK);
994b9b17debSTimur Tabi 	synchronize_irq(adpt->irq.irq);
995b9b17debSTimur Tabi 
99693966b71STimur Tabi 	phy_disconnect(adpt->phydev);
99793966b71STimur Tabi 
998b9b17debSTimur Tabi 	emac_mac_reset(adpt);
999b9b17debSTimur Tabi 
1000b9b17debSTimur Tabi 	emac_tx_q_descs_free(adpt);
1001b9b17debSTimur Tabi 	netdev_reset_queue(adpt->netdev);
1002b9b17debSTimur Tabi 	emac_rx_q_free_descs(adpt);
1003b9b17debSTimur Tabi }
1004b9b17debSTimur Tabi 
1005b9b17debSTimur Tabi /* Consume next received packet descriptor */
1006b9b17debSTimur Tabi static bool emac_rx_process_rrd(struct emac_adapter *adpt,
1007b9b17debSTimur Tabi 				struct emac_rx_queue *rx_q,
1008b9b17debSTimur Tabi 				struct emac_rrd *rrd)
1009b9b17debSTimur Tabi {
1010b9b17debSTimur Tabi 	u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
1011b9b17debSTimur Tabi 
1012b9b17debSTimur Tabi 	rrd->word[3] = *(hw_rrd + 3);
1013b9b17debSTimur Tabi 
1014b9b17debSTimur Tabi 	if (!RRD_UPDT(rrd))
1015b9b17debSTimur Tabi 		return false;
1016b9b17debSTimur Tabi 
1017b9b17debSTimur Tabi 	rrd->word[4] = 0;
1018b9b17debSTimur Tabi 	rrd->word[5] = 0;
1019b9b17debSTimur Tabi 
1020b9b17debSTimur Tabi 	rrd->word[0] = *(hw_rrd++);
1021b9b17debSTimur Tabi 	rrd->word[1] = *(hw_rrd++);
1022b9b17debSTimur Tabi 	rrd->word[2] = *(hw_rrd++);
1023b9b17debSTimur Tabi 
1024b9b17debSTimur Tabi 	if (unlikely(RRD_NOR(rrd) != 1)) {
1025b9b17debSTimur Tabi 		netdev_err(adpt->netdev,
1026b9b17debSTimur Tabi 			   "error: multi-RFD not support yet! nor:%lu\n",
1027b9b17debSTimur Tabi 			   RRD_NOR(rrd));
1028b9b17debSTimur Tabi 	}
1029b9b17debSTimur Tabi 
1030b9b17debSTimur Tabi 	/* mark rrd as processed */
1031b9b17debSTimur Tabi 	RRD_UPDT_SET(rrd, 0);
1032b9b17debSTimur Tabi 	*hw_rrd = rrd->word[3];
1033b9b17debSTimur Tabi 
1034b9b17debSTimur Tabi 	if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
1035b9b17debSTimur Tabi 		rx_q->rrd.consume_idx = 0;
1036b9b17debSTimur Tabi 
1037b9b17debSTimur Tabi 	return true;
1038b9b17debSTimur Tabi }
1039b9b17debSTimur Tabi 
1040b9b17debSTimur Tabi /* Produce new transmit descriptor */
1041b9b17debSTimur Tabi static void emac_tx_tpd_create(struct emac_adapter *adpt,
1042b9b17debSTimur Tabi 			       struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
1043b9b17debSTimur Tabi {
1044b9b17debSTimur Tabi 	u32 *hw_tpd;
1045b9b17debSTimur Tabi 
1046b9b17debSTimur Tabi 	tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
1047b9b17debSTimur Tabi 	hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
1048b9b17debSTimur Tabi 
1049b9b17debSTimur Tabi 	if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
1050b9b17debSTimur Tabi 		tx_q->tpd.produce_idx = 0;
1051b9b17debSTimur Tabi 
1052b9b17debSTimur Tabi 	*(hw_tpd++) = tpd->word[0];
1053b9b17debSTimur Tabi 	*(hw_tpd++) = tpd->word[1];
1054b9b17debSTimur Tabi 	*(hw_tpd++) = tpd->word[2];
1055b9b17debSTimur Tabi 	*hw_tpd = tpd->word[3];
1056b9b17debSTimur Tabi }
1057b9b17debSTimur Tabi 
1058b9b17debSTimur Tabi /* Mark the last transmit descriptor as such (for the transmit packet) */
1059b9b17debSTimur Tabi static void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
1060b9b17debSTimur Tabi 				  struct emac_tx_queue *tx_q)
1061b9b17debSTimur Tabi {
1062b9b17debSTimur Tabi 	u32 *hw_tpd =
1063b9b17debSTimur Tabi 		EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
1064b9b17debSTimur Tabi 	u32 tmp_tpd;
1065b9b17debSTimur Tabi 
1066b9b17debSTimur Tabi 	tmp_tpd = *(hw_tpd + 1);
1067b9b17debSTimur Tabi 	tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
1068b9b17debSTimur Tabi 	*(hw_tpd + 1) = tmp_tpd;
1069b9b17debSTimur Tabi }
1070b9b17debSTimur Tabi 
1071b9b17debSTimur Tabi static void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
1072b9b17debSTimur Tabi {
1073b9b17debSTimur Tabi 	struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
1074b9b17debSTimur Tabi 	u32 consume_idx = RRD_SI(rrd);
1075b9b17debSTimur Tabi 	unsigned int i;
1076b9b17debSTimur Tabi 
1077b9b17debSTimur Tabi 	for (i = 0; i < RRD_NOR(rrd); i++) {
1078b9b17debSTimur Tabi 		rfbuf[consume_idx].skb = NULL;
1079b9b17debSTimur Tabi 		if (++consume_idx == rx_q->rfd.count)
1080b9b17debSTimur Tabi 			consume_idx = 0;
1081b9b17debSTimur Tabi 	}
1082b9b17debSTimur Tabi 
1083b9b17debSTimur Tabi 	rx_q->rfd.consume_idx = consume_idx;
1084b9b17debSTimur Tabi 	rx_q->rfd.process_idx = consume_idx;
1085b9b17debSTimur Tabi }
1086b9b17debSTimur Tabi 
1087b9b17debSTimur Tabi /* Push the received skb to upper layers */
1088b9b17debSTimur Tabi static void emac_receive_skb(struct emac_rx_queue *rx_q,
1089b9b17debSTimur Tabi 			     struct sk_buff *skb,
1090b9b17debSTimur Tabi 			     u16 vlan_tag, bool vlan_flag)
1091b9b17debSTimur Tabi {
1092b9b17debSTimur Tabi 	if (vlan_flag) {
1093b9b17debSTimur Tabi 		u16 vlan;
1094b9b17debSTimur Tabi 
1095b9b17debSTimur Tabi 		EMAC_TAG_TO_VLAN(vlan_tag, vlan);
1096b9b17debSTimur Tabi 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
1097b9b17debSTimur Tabi 	}
1098b9b17debSTimur Tabi 
1099b9b17debSTimur Tabi 	napi_gro_receive(&rx_q->napi, skb);
1100b9b17debSTimur Tabi }
1101b9b17debSTimur Tabi 
1102b9b17debSTimur Tabi /* Process receive event */
1103b9b17debSTimur Tabi void emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
1104b9b17debSTimur Tabi 			 int *num_pkts, int max_pkts)
1105b9b17debSTimur Tabi {
1106b9b17debSTimur Tabi 	u32 proc_idx, hw_consume_idx, num_consume_pkts;
1107b9b17debSTimur Tabi 	struct net_device *netdev  = adpt->netdev;
1108b9b17debSTimur Tabi 	struct emac_buffer *rfbuf;
1109b9b17debSTimur Tabi 	unsigned int count = 0;
1110b9b17debSTimur Tabi 	struct emac_rrd rrd;
1111b9b17debSTimur Tabi 	struct sk_buff *skb;
1112b9b17debSTimur Tabi 	u32 reg;
1113b9b17debSTimur Tabi 
1114b9b17debSTimur Tabi 	reg = readl_relaxed(adpt->base + rx_q->consume_reg);
1115b9b17debSTimur Tabi 
1116b9b17debSTimur Tabi 	hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
1117b9b17debSTimur Tabi 	num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
1118b9b17debSTimur Tabi 		(hw_consume_idx -  rx_q->rrd.consume_idx) :
1119b9b17debSTimur Tabi 		(hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
1120b9b17debSTimur Tabi 
1121b9b17debSTimur Tabi 	do {
1122b9b17debSTimur Tabi 		if (!num_consume_pkts)
1123b9b17debSTimur Tabi 			break;
1124b9b17debSTimur Tabi 
1125b9b17debSTimur Tabi 		if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
1126b9b17debSTimur Tabi 			break;
1127b9b17debSTimur Tabi 
1128b9b17debSTimur Tabi 		if (likely(RRD_NOR(&rrd) == 1)) {
1129b9b17debSTimur Tabi 			/* good receive */
1130b9b17debSTimur Tabi 			rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
1131b9b17debSTimur Tabi 			dma_unmap_single(adpt->netdev->dev.parent,
1132b9b17debSTimur Tabi 					 rfbuf->dma_addr, rfbuf->length,
1133b9b17debSTimur Tabi 					 DMA_FROM_DEVICE);
1134b9b17debSTimur Tabi 			rfbuf->dma_addr = 0;
1135b9b17debSTimur Tabi 			skb = rfbuf->skb;
1136b9b17debSTimur Tabi 		} else {
1137b9b17debSTimur Tabi 			netdev_err(adpt->netdev,
1138b9b17debSTimur Tabi 				   "error: multi-RFD not support yet!\n");
1139b9b17debSTimur Tabi 			break;
1140b9b17debSTimur Tabi 		}
1141b9b17debSTimur Tabi 		emac_rx_rfd_clean(rx_q, &rrd);
1142b9b17debSTimur Tabi 		num_consume_pkts--;
1143b9b17debSTimur Tabi 		count++;
1144b9b17debSTimur Tabi 
1145b9b17debSTimur Tabi 		/* Due to a HW issue in L4 check sum detection (UDP/TCP frags
1146b9b17debSTimur Tabi 		 * with DF set are marked as error), drop packets based on the
1147b9b17debSTimur Tabi 		 * error mask rather than the summary bit (ignoring L4F errors)
1148b9b17debSTimur Tabi 		 */
1149b9b17debSTimur Tabi 		if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
1150b9b17debSTimur Tabi 			netif_dbg(adpt, rx_status, adpt->netdev,
1151b9b17debSTimur Tabi 				  "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
1152b9b17debSTimur Tabi 				  rrd.word[0], rrd.word[1],
1153b9b17debSTimur Tabi 				  rrd.word[2], rrd.word[3]);
1154b9b17debSTimur Tabi 
1155b9b17debSTimur Tabi 			dev_kfree_skb(skb);
1156b9b17debSTimur Tabi 			continue;
1157b9b17debSTimur Tabi 		}
1158b9b17debSTimur Tabi 
1159b9b17debSTimur Tabi 		skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
1160b9b17debSTimur Tabi 		skb->dev = netdev;
1161b9b17debSTimur Tabi 		skb->protocol = eth_type_trans(skb, skb->dev);
1162b9b17debSTimur Tabi 		if (netdev->features & NETIF_F_RXCSUM)
1163b9b17debSTimur Tabi 			skb->ip_summed = RRD_L4F(&rrd) ?
1164b9b17debSTimur Tabi 					  CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1165b9b17debSTimur Tabi 		else
1166b9b17debSTimur Tabi 			skb_checksum_none_assert(skb);
1167b9b17debSTimur Tabi 
1168b9b17debSTimur Tabi 		emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
1169b9b17debSTimur Tabi 				 (bool)RRD_CVTAG(&rrd));
1170b9b17debSTimur Tabi 
1171b9b17debSTimur Tabi 		(*num_pkts)++;
1172b9b17debSTimur Tabi 	} while (*num_pkts < max_pkts);
1173b9b17debSTimur Tabi 
1174b9b17debSTimur Tabi 	if (count) {
1175b9b17debSTimur Tabi 		proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
1176b9b17debSTimur Tabi 				rx_q->process_mask;
1177b9b17debSTimur Tabi 		emac_reg_update32(adpt->base + rx_q->process_reg,
1178b9b17debSTimur Tabi 				  rx_q->process_mask, proc_idx);
1179b9b17debSTimur Tabi 		emac_mac_rx_descs_refill(adpt, rx_q);
1180b9b17debSTimur Tabi 	}
1181b9b17debSTimur Tabi }
1182b9b17debSTimur Tabi 
1183b9b17debSTimur Tabi /* get the number of free transmit descriptors */
1184b9b17debSTimur Tabi static unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
1185b9b17debSTimur Tabi {
1186b9b17debSTimur Tabi 	u32 produce_idx = tx_q->tpd.produce_idx;
1187b9b17debSTimur Tabi 	u32 consume_idx = tx_q->tpd.consume_idx;
1188b9b17debSTimur Tabi 
1189b9b17debSTimur Tabi 	return (consume_idx > produce_idx) ?
1190b9b17debSTimur Tabi 		(consume_idx - produce_idx - 1) :
1191b9b17debSTimur Tabi 		(tx_q->tpd.count + consume_idx - produce_idx - 1);
1192b9b17debSTimur Tabi }
1193b9b17debSTimur Tabi 
1194b9b17debSTimur Tabi /* Process transmit event */
1195b9b17debSTimur Tabi void emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
1196b9b17debSTimur Tabi {
1197b9b17debSTimur Tabi 	u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
1198b9b17debSTimur Tabi 	u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
1199b9b17debSTimur Tabi 	struct emac_buffer *tpbuf;
1200b9b17debSTimur Tabi 
1201b9b17debSTimur Tabi 	hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
1202b9b17debSTimur Tabi 
1203b9b17debSTimur Tabi 	while (tx_q->tpd.consume_idx != hw_consume_idx) {
1204b9b17debSTimur Tabi 		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
1205b9b17debSTimur Tabi 		if (tpbuf->dma_addr) {
1206b9b17debSTimur Tabi 			dma_unmap_single(adpt->netdev->dev.parent,
1207b9b17debSTimur Tabi 					 tpbuf->dma_addr, tpbuf->length,
1208b9b17debSTimur Tabi 					 DMA_TO_DEVICE);
1209b9b17debSTimur Tabi 			tpbuf->dma_addr = 0;
1210b9b17debSTimur Tabi 		}
1211b9b17debSTimur Tabi 
1212b9b17debSTimur Tabi 		if (tpbuf->skb) {
1213b9b17debSTimur Tabi 			pkts_compl++;
1214b9b17debSTimur Tabi 			bytes_compl += tpbuf->skb->len;
1215b9b17debSTimur Tabi 			dev_kfree_skb_irq(tpbuf->skb);
1216b9b17debSTimur Tabi 			tpbuf->skb = NULL;
1217b9b17debSTimur Tabi 		}
1218b9b17debSTimur Tabi 
1219b9b17debSTimur Tabi 		if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
1220b9b17debSTimur Tabi 			tx_q->tpd.consume_idx = 0;
1221b9b17debSTimur Tabi 	}
1222b9b17debSTimur Tabi 
1223b9b17debSTimur Tabi 	netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
1224b9b17debSTimur Tabi 
1225b9b17debSTimur Tabi 	if (netif_queue_stopped(adpt->netdev))
1226b9b17debSTimur Tabi 		if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
1227b9b17debSTimur Tabi 			netif_wake_queue(adpt->netdev);
1228b9b17debSTimur Tabi }
1229b9b17debSTimur Tabi 
1230b9b17debSTimur Tabi /* Initialize all queue data structures */
1231b9b17debSTimur Tabi void emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
1232b9b17debSTimur Tabi 				  struct emac_adapter *adpt)
1233b9b17debSTimur Tabi {
1234b9b17debSTimur Tabi 	adpt->rx_q.netdev = adpt->netdev;
1235b9b17debSTimur Tabi 
1236b9b17debSTimur Tabi 	adpt->rx_q.produce_reg  = EMAC_MAILBOX_0;
1237b9b17debSTimur Tabi 	adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
1238b9b17debSTimur Tabi 	adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
1239b9b17debSTimur Tabi 
1240b9b17debSTimur Tabi 	adpt->rx_q.process_reg  = EMAC_MAILBOX_0;
1241b9b17debSTimur Tabi 	adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
1242b9b17debSTimur Tabi 	adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
1243b9b17debSTimur Tabi 
1244b9b17debSTimur Tabi 	adpt->rx_q.consume_reg  = EMAC_MAILBOX_3;
1245b9b17debSTimur Tabi 	adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
1246b9b17debSTimur Tabi 	adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
1247b9b17debSTimur Tabi 
1248b9b17debSTimur Tabi 	adpt->rx_q.irq          = &adpt->irq;
1249b9b17debSTimur Tabi 	adpt->rx_q.intr         = adpt->irq.mask & ISR_RX_PKT;
1250b9b17debSTimur Tabi 
1251b9b17debSTimur Tabi 	adpt->tx_q.produce_reg  = EMAC_MAILBOX_15;
1252b9b17debSTimur Tabi 	adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
1253b9b17debSTimur Tabi 	adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
1254b9b17debSTimur Tabi 
1255b9b17debSTimur Tabi 	adpt->tx_q.consume_reg  = EMAC_MAILBOX_2;
1256b9b17debSTimur Tabi 	adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
1257b9b17debSTimur Tabi 	adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
1258b9b17debSTimur Tabi }
1259b9b17debSTimur Tabi 
1260b9b17debSTimur Tabi /* Fill up transmit descriptors with TSO and Checksum offload information */
1261b9b17debSTimur Tabi static int emac_tso_csum(struct emac_adapter *adpt,
1262b9b17debSTimur Tabi 			 struct emac_tx_queue *tx_q,
1263b9b17debSTimur Tabi 			 struct sk_buff *skb,
1264b9b17debSTimur Tabi 			 struct emac_tpd *tpd)
1265b9b17debSTimur Tabi {
1266b9b17debSTimur Tabi 	unsigned int hdr_len;
1267b9b17debSTimur Tabi 	int ret;
1268b9b17debSTimur Tabi 
1269b9b17debSTimur Tabi 	if (skb_is_gso(skb)) {
1270b9b17debSTimur Tabi 		if (skb_header_cloned(skb)) {
1271b9b17debSTimur Tabi 			ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1272b9b17debSTimur Tabi 			if (unlikely(ret))
1273b9b17debSTimur Tabi 				return ret;
1274b9b17debSTimur Tabi 		}
1275b9b17debSTimur Tabi 
1276b9b17debSTimur Tabi 		if (skb->protocol == htons(ETH_P_IP)) {
1277b9b17debSTimur Tabi 			u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
1278b9b17debSTimur Tabi 				       + ntohs(ip_hdr(skb)->tot_len);
1279b9b17debSTimur Tabi 			if (skb->len > pkt_len)
1280b9b17debSTimur Tabi 				pskb_trim(skb, pkt_len);
1281b9b17debSTimur Tabi 		}
1282b9b17debSTimur Tabi 
1283b9b17debSTimur Tabi 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1284b9b17debSTimur Tabi 		if (unlikely(skb->len == hdr_len)) {
1285b9b17debSTimur Tabi 			/* we only need to do csum */
1286b9b17debSTimur Tabi 			netif_warn(adpt, tx_err, adpt->netdev,
1287b9b17debSTimur Tabi 				   "tso not needed for packet with 0 data\n");
1288b9b17debSTimur Tabi 			goto do_csum;
1289b9b17debSTimur Tabi 		}
1290b9b17debSTimur Tabi 
1291b9b17debSTimur Tabi 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1292b9b17debSTimur Tabi 			ip_hdr(skb)->check = 0;
1293b9b17debSTimur Tabi 			tcp_hdr(skb)->check =
1294b9b17debSTimur Tabi 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1295b9b17debSTimur Tabi 						   ip_hdr(skb)->daddr,
1296b9b17debSTimur Tabi 						   0, IPPROTO_TCP, 0);
1297b9b17debSTimur Tabi 			TPD_IPV4_SET(tpd, 1);
1298b9b17debSTimur Tabi 		}
1299b9b17debSTimur Tabi 
1300b9b17debSTimur Tabi 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1301b9b17debSTimur Tabi 			/* ipv6 tso need an extra tpd */
1302b9b17debSTimur Tabi 			struct emac_tpd extra_tpd;
1303b9b17debSTimur Tabi 
1304b9b17debSTimur Tabi 			memset(tpd, 0, sizeof(*tpd));
1305b9b17debSTimur Tabi 			memset(&extra_tpd, 0, sizeof(extra_tpd));
1306b9b17debSTimur Tabi 
1307b9b17debSTimur Tabi 			ipv6_hdr(skb)->payload_len = 0;
1308b9b17debSTimur Tabi 			tcp_hdr(skb)->check =
1309b9b17debSTimur Tabi 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1310b9b17debSTimur Tabi 						 &ipv6_hdr(skb)->daddr,
1311b9b17debSTimur Tabi 						 0, IPPROTO_TCP, 0);
1312b9b17debSTimur Tabi 			TPD_PKT_LEN_SET(&extra_tpd, skb->len);
1313b9b17debSTimur Tabi 			TPD_LSO_SET(&extra_tpd, 1);
1314b9b17debSTimur Tabi 			TPD_LSOV_SET(&extra_tpd, 1);
1315b9b17debSTimur Tabi 			emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
1316b9b17debSTimur Tabi 			TPD_LSOV_SET(tpd, 1);
1317b9b17debSTimur Tabi 		}
1318b9b17debSTimur Tabi 
1319b9b17debSTimur Tabi 		TPD_LSO_SET(tpd, 1);
1320b9b17debSTimur Tabi 		TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
1321b9b17debSTimur Tabi 		TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
1322b9b17debSTimur Tabi 		return 0;
1323b9b17debSTimur Tabi 	}
1324b9b17debSTimur Tabi 
1325b9b17debSTimur Tabi do_csum:
1326b9b17debSTimur Tabi 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1327b9b17debSTimur Tabi 		unsigned int css, cso;
1328b9b17debSTimur Tabi 
1329b9b17debSTimur Tabi 		cso = skb_transport_offset(skb);
1330b9b17debSTimur Tabi 		if (unlikely(cso & 0x1)) {
1331b9b17debSTimur Tabi 			netdev_err(adpt->netdev,
1332b9b17debSTimur Tabi 				   "error: payload offset should be even\n");
1333b9b17debSTimur Tabi 			return -EINVAL;
1334b9b17debSTimur Tabi 		}
1335b9b17debSTimur Tabi 		css = cso + skb->csum_offset;
1336b9b17debSTimur Tabi 
1337b9b17debSTimur Tabi 		TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
1338b9b17debSTimur Tabi 		TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
1339b9b17debSTimur Tabi 		TPD_CSX_SET(tpd, 1);
1340b9b17debSTimur Tabi 	}
1341b9b17debSTimur Tabi 
1342b9b17debSTimur Tabi 	return 0;
1343b9b17debSTimur Tabi }
1344b9b17debSTimur Tabi 
1345b9b17debSTimur Tabi /* Fill up transmit descriptors */
1346b9b17debSTimur Tabi static void emac_tx_fill_tpd(struct emac_adapter *adpt,
1347b9b17debSTimur Tabi 			     struct emac_tx_queue *tx_q, struct sk_buff *skb,
1348b9b17debSTimur Tabi 			     struct emac_tpd *tpd)
1349b9b17debSTimur Tabi {
1350b9b17debSTimur Tabi 	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
1351b9b17debSTimur Tabi 	unsigned int first = tx_q->tpd.produce_idx;
1352b9b17debSTimur Tabi 	unsigned int len = skb_headlen(skb);
1353b9b17debSTimur Tabi 	struct emac_buffer *tpbuf = NULL;
1354b9b17debSTimur Tabi 	unsigned int mapped_len = 0;
1355b9b17debSTimur Tabi 	unsigned int i;
1356b9b17debSTimur Tabi 	int count = 0;
1357b9b17debSTimur Tabi 	int ret;
1358b9b17debSTimur Tabi 
1359b9b17debSTimur Tabi 	/* if Large Segment Offload is (in TCP Segmentation Offload struct) */
1360b9b17debSTimur Tabi 	if (TPD_LSO(tpd)) {
1361b9b17debSTimur Tabi 		mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1362b9b17debSTimur Tabi 
1363b9b17debSTimur Tabi 		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1364b9b17debSTimur Tabi 		tpbuf->length = mapped_len;
1365b9b17debSTimur Tabi 		tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
1366b9b17debSTimur Tabi 						 skb->data, tpbuf->length,
1367b9b17debSTimur Tabi 						 DMA_TO_DEVICE);
1368b9b17debSTimur Tabi 		ret = dma_mapping_error(adpt->netdev->dev.parent,
1369b9b17debSTimur Tabi 					tpbuf->dma_addr);
1370b9b17debSTimur Tabi 		if (ret)
1371b9b17debSTimur Tabi 			goto error;
1372b9b17debSTimur Tabi 
1373b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1374b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1375b9b17debSTimur Tabi 		TPD_BUF_LEN_SET(tpd, tpbuf->length);
1376b9b17debSTimur Tabi 		emac_tx_tpd_create(adpt, tx_q, tpd);
1377b9b17debSTimur Tabi 		count++;
1378b9b17debSTimur Tabi 	}
1379b9b17debSTimur Tabi 
1380b9b17debSTimur Tabi 	if (mapped_len < len) {
1381b9b17debSTimur Tabi 		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1382b9b17debSTimur Tabi 		tpbuf->length = len - mapped_len;
1383b9b17debSTimur Tabi 		tpbuf->dma_addr = dma_map_single(adpt->netdev->dev.parent,
1384b9b17debSTimur Tabi 						 skb->data + mapped_len,
1385b9b17debSTimur Tabi 						 tpbuf->length, DMA_TO_DEVICE);
1386b9b17debSTimur Tabi 		ret = dma_mapping_error(adpt->netdev->dev.parent,
1387b9b17debSTimur Tabi 					tpbuf->dma_addr);
1388b9b17debSTimur Tabi 		if (ret)
1389b9b17debSTimur Tabi 			goto error;
1390b9b17debSTimur Tabi 
1391b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1392b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1393b9b17debSTimur Tabi 		TPD_BUF_LEN_SET(tpd, tpbuf->length);
1394b9b17debSTimur Tabi 		emac_tx_tpd_create(adpt, tx_q, tpd);
1395b9b17debSTimur Tabi 		count++;
1396b9b17debSTimur Tabi 	}
1397b9b17debSTimur Tabi 
1398b9b17debSTimur Tabi 	for (i = 0; i < nr_frags; i++) {
1399b9b17debSTimur Tabi 		struct skb_frag_struct *frag;
1400b9b17debSTimur Tabi 
1401b9b17debSTimur Tabi 		frag = &skb_shinfo(skb)->frags[i];
1402b9b17debSTimur Tabi 
1403b9b17debSTimur Tabi 		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
1404b9b17debSTimur Tabi 		tpbuf->length = frag->size;
1405b9b17debSTimur Tabi 		tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
1406b9b17debSTimur Tabi 					       frag->page.p, frag->page_offset,
1407b9b17debSTimur Tabi 					       tpbuf->length, DMA_TO_DEVICE);
1408b9b17debSTimur Tabi 		ret = dma_mapping_error(adpt->netdev->dev.parent,
1409b9b17debSTimur Tabi 					tpbuf->dma_addr);
1410b9b17debSTimur Tabi 		if (ret)
1411b9b17debSTimur Tabi 			goto error;
1412b9b17debSTimur Tabi 
1413b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
1414b9b17debSTimur Tabi 		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
1415b9b17debSTimur Tabi 		TPD_BUF_LEN_SET(tpd, tpbuf->length);
1416b9b17debSTimur Tabi 		emac_tx_tpd_create(adpt, tx_q, tpd);
1417b9b17debSTimur Tabi 		count++;
1418b9b17debSTimur Tabi 	}
1419b9b17debSTimur Tabi 
1420b9b17debSTimur Tabi 	/* The last tpd */
1421b9b17debSTimur Tabi 	wmb();
1422b9b17debSTimur Tabi 	emac_tx_tpd_mark_last(adpt, tx_q);
1423b9b17debSTimur Tabi 
1424b9b17debSTimur Tabi 	/* The last buffer info contain the skb address,
1425b9b17debSTimur Tabi 	 * so it will be freed after unmap
1426b9b17debSTimur Tabi 	 */
1427b9b17debSTimur Tabi 	tpbuf->skb = skb;
1428b9b17debSTimur Tabi 
1429b9b17debSTimur Tabi 	return;
1430b9b17debSTimur Tabi 
1431b9b17debSTimur Tabi error:
1432b9b17debSTimur Tabi 	/* One of the memory mappings failed, so undo everything */
1433b9b17debSTimur Tabi 	tx_q->tpd.produce_idx = first;
1434b9b17debSTimur Tabi 
1435b9b17debSTimur Tabi 	while (count--) {
1436b9b17debSTimur Tabi 		tpbuf = GET_TPD_BUFFER(tx_q, first);
1437b9b17debSTimur Tabi 		dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
1438b9b17debSTimur Tabi 			       tpbuf->length, DMA_TO_DEVICE);
1439b9b17debSTimur Tabi 		tpbuf->dma_addr = 0;
1440b9b17debSTimur Tabi 		tpbuf->length = 0;
1441b9b17debSTimur Tabi 
1442b9b17debSTimur Tabi 		if (++first == tx_q->tpd.count)
1443b9b17debSTimur Tabi 			first = 0;
1444b9b17debSTimur Tabi 	}
1445b9b17debSTimur Tabi 
1446b9b17debSTimur Tabi 	dev_kfree_skb(skb);
1447b9b17debSTimur Tabi }
1448b9b17debSTimur Tabi 
1449b9b17debSTimur Tabi /* Transmit the packet using specified transmit queue */
1450b9b17debSTimur Tabi int emac_mac_tx_buf_send(struct emac_adapter *adpt, struct emac_tx_queue *tx_q,
1451b9b17debSTimur Tabi 			 struct sk_buff *skb)
1452b9b17debSTimur Tabi {
1453b9b17debSTimur Tabi 	struct emac_tpd tpd;
1454b9b17debSTimur Tabi 	u32 prod_idx;
1455b9b17debSTimur Tabi 
1456b9b17debSTimur Tabi 	memset(&tpd, 0, sizeof(tpd));
1457b9b17debSTimur Tabi 
1458b9b17debSTimur Tabi 	if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
1459b9b17debSTimur Tabi 		dev_kfree_skb_any(skb);
1460b9b17debSTimur Tabi 		return NETDEV_TX_OK;
1461b9b17debSTimur Tabi 	}
1462b9b17debSTimur Tabi 
1463b9b17debSTimur Tabi 	if (skb_vlan_tag_present(skb)) {
1464b9b17debSTimur Tabi 		u16 tag;
1465b9b17debSTimur Tabi 
1466b9b17debSTimur Tabi 		EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
1467b9b17debSTimur Tabi 		TPD_CVLAN_TAG_SET(&tpd, tag);
1468b9b17debSTimur Tabi 		TPD_INSTC_SET(&tpd, 1);
1469b9b17debSTimur Tabi 	}
1470b9b17debSTimur Tabi 
1471b9b17debSTimur Tabi 	if (skb_network_offset(skb) != ETH_HLEN)
1472b9b17debSTimur Tabi 		TPD_TYP_SET(&tpd, 1);
1473b9b17debSTimur Tabi 
1474b9b17debSTimur Tabi 	emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
1475b9b17debSTimur Tabi 
1476b9b17debSTimur Tabi 	netdev_sent_queue(adpt->netdev, skb->len);
1477b9b17debSTimur Tabi 
1478b9b17debSTimur Tabi 	/* Make sure the are enough free descriptors to hold one
1479b9b17debSTimur Tabi 	 * maximum-sized SKB.  We need one desc for each fragment,
1480b9b17debSTimur Tabi 	 * one for the checksum (emac_tso_csum), one for TSO, and
1481b9b17debSTimur Tabi 	 * and one for the SKB header.
1482b9b17debSTimur Tabi 	 */
1483b9b17debSTimur Tabi 	if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
1484b9b17debSTimur Tabi 		netif_stop_queue(adpt->netdev);
1485b9b17debSTimur Tabi 
1486b9b17debSTimur Tabi 	/* update produce idx */
1487b9b17debSTimur Tabi 	prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
1488b9b17debSTimur Tabi 		    tx_q->produce_mask;
1489b9b17debSTimur Tabi 	emac_reg_update32(adpt->base + tx_q->produce_reg,
1490b9b17debSTimur Tabi 			  tx_q->produce_mask, prod_idx);
1491b9b17debSTimur Tabi 
1492b9b17debSTimur Tabi 	return NETDEV_TX_OK;
1493b9b17debSTimur Tabi }
1494