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