18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/* Qualcomm Technologies, Inc. EMAC Ethernet Controller MAC layer support
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/tcp.h>
98c2ecf20Sopenharmony_ci#include <linux/ip.h>
108c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
118c2ecf20Sopenharmony_ci#include <linux/crc32.h>
128c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
138c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
148c2ecf20Sopenharmony_ci#include <linux/phy.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <net/ip6_checksum.h>
178c2ecf20Sopenharmony_ci#include "emac.h"
188c2ecf20Sopenharmony_ci#include "emac-sgmii.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* EMAC_MAC_CTRL */
218c2ecf20Sopenharmony_ci#define SINGLE_PAUSE_MODE       	0x10000000
228c2ecf20Sopenharmony_ci#define DEBUG_MODE                      0x08000000
238c2ecf20Sopenharmony_ci#define BROAD_EN                        0x04000000
248c2ecf20Sopenharmony_ci#define MULTI_ALL                       0x02000000
258c2ecf20Sopenharmony_ci#define RX_CHKSUM_EN                    0x01000000
268c2ecf20Sopenharmony_ci#define HUGE                            0x00800000
278c2ecf20Sopenharmony_ci#define SPEED(x)			(((x) & 0x3) << 20)
288c2ecf20Sopenharmony_ci#define SPEED_MASK			SPEED(0x3)
298c2ecf20Sopenharmony_ci#define SIMR                            0x00080000
308c2ecf20Sopenharmony_ci#define TPAUSE                          0x00010000
318c2ecf20Sopenharmony_ci#define PROM_MODE                       0x00008000
328c2ecf20Sopenharmony_ci#define VLAN_STRIP                      0x00004000
338c2ecf20Sopenharmony_ci#define PRLEN_BMSK                      0x00003c00
348c2ecf20Sopenharmony_ci#define PRLEN_SHFT                      10
358c2ecf20Sopenharmony_ci#define HUGEN                           0x00000200
368c2ecf20Sopenharmony_ci#define FLCHK                           0x00000100
378c2ecf20Sopenharmony_ci#define PCRCE                           0x00000080
388c2ecf20Sopenharmony_ci#define CRCE                            0x00000040
398c2ecf20Sopenharmony_ci#define FULLD                           0x00000020
408c2ecf20Sopenharmony_ci#define MAC_LP_EN                       0x00000010
418c2ecf20Sopenharmony_ci#define RXFC                            0x00000008
428c2ecf20Sopenharmony_ci#define TXFC                            0x00000004
438c2ecf20Sopenharmony_ci#define RXEN                            0x00000002
448c2ecf20Sopenharmony_ci#define TXEN                            0x00000001
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* EMAC_DESC_CTRL_3 */
478c2ecf20Sopenharmony_ci#define RFD_RING_SIZE_BMSK                                       0xfff
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* EMAC_DESC_CTRL_4 */
508c2ecf20Sopenharmony_ci#define RX_BUFFER_SIZE_BMSK                                     0xffff
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* EMAC_DESC_CTRL_6 */
538c2ecf20Sopenharmony_ci#define RRD_RING_SIZE_BMSK                                       0xfff
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* EMAC_DESC_CTRL_9 */
568c2ecf20Sopenharmony_ci#define TPD_RING_SIZE_BMSK                                      0xffff
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* EMAC_TXQ_CTRL_0 */
598c2ecf20Sopenharmony_ci#define NUM_TXF_BURST_PREF_BMSK                             0xffff0000
608c2ecf20Sopenharmony_ci#define NUM_TXF_BURST_PREF_SHFT                                     16
618c2ecf20Sopenharmony_ci#define LS_8023_SP                                                0x80
628c2ecf20Sopenharmony_ci#define TXQ_MODE                                                  0x40
638c2ecf20Sopenharmony_ci#define TXQ_EN                                                    0x20
648c2ecf20Sopenharmony_ci#define IP_OP_SP                                                  0x10
658c2ecf20Sopenharmony_ci#define NUM_TPD_BURST_PREF_BMSK                                    0xf
668c2ecf20Sopenharmony_ci#define NUM_TPD_BURST_PREF_SHFT                                      0
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* EMAC_TXQ_CTRL_1 */
698c2ecf20Sopenharmony_ci#define JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK                        0x7ff
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* EMAC_TXQ_CTRL_2 */
728c2ecf20Sopenharmony_ci#define TXF_HWM_BMSK                                         0xfff0000
738c2ecf20Sopenharmony_ci#define TXF_LWM_BMSK                                             0xfff
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* EMAC_RXQ_CTRL_0 */
768c2ecf20Sopenharmony_ci#define RXQ_EN                                                 BIT(31)
778c2ecf20Sopenharmony_ci#define CUT_THRU_EN                                            BIT(30)
788c2ecf20Sopenharmony_ci#define RSS_HASH_EN                                            BIT(29)
798c2ecf20Sopenharmony_ci#define NUM_RFD_BURST_PREF_BMSK                              0x3f00000
808c2ecf20Sopenharmony_ci#define NUM_RFD_BURST_PREF_SHFT                                     20
818c2ecf20Sopenharmony_ci#define IDT_TABLE_SIZE_BMSK                                    0x1ff00
828c2ecf20Sopenharmony_ci#define IDT_TABLE_SIZE_SHFT                                          8
838c2ecf20Sopenharmony_ci#define SP_IPV6                                                   0x80
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* EMAC_RXQ_CTRL_1 */
868c2ecf20Sopenharmony_ci#define JUMBO_1KAH_BMSK                                         0xf000
878c2ecf20Sopenharmony_ci#define JUMBO_1KAH_SHFT                                             12
888c2ecf20Sopenharmony_ci#define RFD_PREF_LOW_TH                                           0x10
898c2ecf20Sopenharmony_ci#define RFD_PREF_LOW_THRESHOLD_BMSK                              0xfc0
908c2ecf20Sopenharmony_ci#define RFD_PREF_LOW_THRESHOLD_SHFT                                  6
918c2ecf20Sopenharmony_ci#define RFD_PREF_UP_TH                                            0x10
928c2ecf20Sopenharmony_ci#define RFD_PREF_UP_THRESHOLD_BMSK                                0x3f
938c2ecf20Sopenharmony_ci#define RFD_PREF_UP_THRESHOLD_SHFT                                   0
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* EMAC_RXQ_CTRL_2 */
968c2ecf20Sopenharmony_ci#define RXF_DOF_THRESFHOLD                                       0x1a0
978c2ecf20Sopenharmony_ci#define RXF_DOF_THRESHOLD_BMSK                               0xfff0000
988c2ecf20Sopenharmony_ci#define RXF_DOF_THRESHOLD_SHFT                                      16
998c2ecf20Sopenharmony_ci#define RXF_UOF_THRESFHOLD                                        0xbe
1008c2ecf20Sopenharmony_ci#define RXF_UOF_THRESHOLD_BMSK                                   0xfff
1018c2ecf20Sopenharmony_ci#define RXF_UOF_THRESHOLD_SHFT                                       0
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* EMAC_RXQ_CTRL_3 */
1048c2ecf20Sopenharmony_ci#define RXD_TIMER_BMSK                                      0xffff0000
1058c2ecf20Sopenharmony_ci#define RXD_THRESHOLD_BMSK                                       0xfff
1068c2ecf20Sopenharmony_ci#define RXD_THRESHOLD_SHFT                                           0
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* EMAC_DMA_CTRL */
1098c2ecf20Sopenharmony_ci#define DMAW_DLY_CNT_BMSK                                      0xf0000
1108c2ecf20Sopenharmony_ci#define DMAW_DLY_CNT_SHFT                                           16
1118c2ecf20Sopenharmony_ci#define DMAR_DLY_CNT_BMSK                                       0xf800
1128c2ecf20Sopenharmony_ci#define DMAR_DLY_CNT_SHFT                                           11
1138c2ecf20Sopenharmony_ci#define DMAR_REQ_PRI                                             0x400
1148c2ecf20Sopenharmony_ci#define REGWRBLEN_BMSK                                           0x380
1158c2ecf20Sopenharmony_ci#define REGWRBLEN_SHFT                                               7
1168c2ecf20Sopenharmony_ci#define REGRDBLEN_BMSK                                            0x70
1178c2ecf20Sopenharmony_ci#define REGRDBLEN_SHFT                                               4
1188c2ecf20Sopenharmony_ci#define OUT_ORDER_MODE                                             0x4
1198c2ecf20Sopenharmony_ci#define ENH_ORDER_MODE                                             0x2
1208c2ecf20Sopenharmony_ci#define IN_ORDER_MODE                                              0x1
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_13 */
1238c2ecf20Sopenharmony_ci#define RFD3_PROC_IDX_BMSK                                   0xfff0000
1248c2ecf20Sopenharmony_ci#define RFD3_PROC_IDX_SHFT                                          16
1258c2ecf20Sopenharmony_ci#define RFD3_PROD_IDX_BMSK                                       0xfff
1268c2ecf20Sopenharmony_ci#define RFD3_PROD_IDX_SHFT                                           0
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_2 */
1298c2ecf20Sopenharmony_ci#define NTPD_CONS_IDX_BMSK                                  0xffff0000
1308c2ecf20Sopenharmony_ci#define NTPD_CONS_IDX_SHFT                                          16
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_3 */
1338c2ecf20Sopenharmony_ci#define RFD0_CONS_IDX_BMSK                                       0xfff
1348c2ecf20Sopenharmony_ci#define RFD0_CONS_IDX_SHFT                                           0
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_11 */
1378c2ecf20Sopenharmony_ci#define H3TPD_PROD_IDX_BMSK                                 0xffff0000
1388c2ecf20Sopenharmony_ci#define H3TPD_PROD_IDX_SHFT                                         16
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* EMAC_AXI_MAST_CTRL */
1418c2ecf20Sopenharmony_ci#define DATA_BYTE_SWAP                                             0x8
1428c2ecf20Sopenharmony_ci#define MAX_BOUND                                                  0x2
1438c2ecf20Sopenharmony_ci#define MAX_BTYPE                                                  0x1
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_12 */
1468c2ecf20Sopenharmony_ci#define H3TPD_CONS_IDX_BMSK                                 0xffff0000
1478c2ecf20Sopenharmony_ci#define H3TPD_CONS_IDX_SHFT                                         16
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_9 */
1508c2ecf20Sopenharmony_ci#define H2TPD_PROD_IDX_BMSK                                     0xffff
1518c2ecf20Sopenharmony_ci#define H2TPD_PROD_IDX_SHFT                                          0
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_10 */
1548c2ecf20Sopenharmony_ci#define H1TPD_CONS_IDX_BMSK                                 0xffff0000
1558c2ecf20Sopenharmony_ci#define H1TPD_CONS_IDX_SHFT                                         16
1568c2ecf20Sopenharmony_ci#define H2TPD_CONS_IDX_BMSK                                     0xffff
1578c2ecf20Sopenharmony_ci#define H2TPD_CONS_IDX_SHFT                                          0
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* EMAC_ATHR_HEADER_CTRL */
1608c2ecf20Sopenharmony_ci#define HEADER_CNT_EN                                              0x2
1618c2ecf20Sopenharmony_ci#define HEADER_ENABLE                                              0x1
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_0 */
1648c2ecf20Sopenharmony_ci#define RFD0_PROC_IDX_BMSK                                   0xfff0000
1658c2ecf20Sopenharmony_ci#define RFD0_PROC_IDX_SHFT                                          16
1668c2ecf20Sopenharmony_ci#define RFD0_PROD_IDX_BMSK                                       0xfff
1678c2ecf20Sopenharmony_ci#define RFD0_PROD_IDX_SHFT                                           0
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_5 */
1708c2ecf20Sopenharmony_ci#define RFD1_PROC_IDX_BMSK                                   0xfff0000
1718c2ecf20Sopenharmony_ci#define RFD1_PROC_IDX_SHFT                                          16
1728c2ecf20Sopenharmony_ci#define RFD1_PROD_IDX_BMSK                                       0xfff
1738c2ecf20Sopenharmony_ci#define RFD1_PROD_IDX_SHFT                                           0
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* EMAC_MISC_CTRL */
1768c2ecf20Sopenharmony_ci#define RX_UNCPL_INT_EN                                            0x1
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_7 */
1798c2ecf20Sopenharmony_ci#define RFD2_CONS_IDX_BMSK                                   0xfff0000
1808c2ecf20Sopenharmony_ci#define RFD2_CONS_IDX_SHFT                                          16
1818c2ecf20Sopenharmony_ci#define RFD1_CONS_IDX_BMSK                                       0xfff
1828c2ecf20Sopenharmony_ci#define RFD1_CONS_IDX_SHFT                                           0
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_8 */
1858c2ecf20Sopenharmony_ci#define RFD3_CONS_IDX_BMSK                                       0xfff
1868c2ecf20Sopenharmony_ci#define RFD3_CONS_IDX_SHFT                                           0
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_15 */
1898c2ecf20Sopenharmony_ci#define NTPD_PROD_IDX_BMSK                                      0xffff
1908c2ecf20Sopenharmony_ci#define NTPD_PROD_IDX_SHFT                                           0
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* EMAC_MAILBOX_16 */
1938c2ecf20Sopenharmony_ci#define H1TPD_PROD_IDX_BMSK                                     0xffff
1948c2ecf20Sopenharmony_ci#define H1TPD_PROD_IDX_SHFT                                          0
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define RXQ0_RSS_HSTYP_IPV6_TCP_EN                                0x20
1978c2ecf20Sopenharmony_ci#define RXQ0_RSS_HSTYP_IPV6_EN                                    0x10
1988c2ecf20Sopenharmony_ci#define RXQ0_RSS_HSTYP_IPV4_TCP_EN                                 0x8
1998c2ecf20Sopenharmony_ci#define RXQ0_RSS_HSTYP_IPV4_EN                                     0x4
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* EMAC_EMAC_WRAPPER_TX_TS_INX */
2028c2ecf20Sopenharmony_ci#define EMAC_WRAPPER_TX_TS_EMPTY                               BIT(31)
2038c2ecf20Sopenharmony_ci#define EMAC_WRAPPER_TX_TS_INX_BMSK                             0xffff
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistruct emac_skb_cb {
2068c2ecf20Sopenharmony_ci	u32           tpd_idx;
2078c2ecf20Sopenharmony_ci	unsigned long jiffies;
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#define EMAC_SKB_CB(skb)	((struct emac_skb_cb *)(skb)->cb)
2118c2ecf20Sopenharmony_ci#define EMAC_RSS_IDT_SIZE	256
2128c2ecf20Sopenharmony_ci#define JUMBO_1KAH		0x4
2138c2ecf20Sopenharmony_ci#define RXD_TH			0x100
2148c2ecf20Sopenharmony_ci#define EMAC_TPD_LAST_FRAGMENT	0x80000000
2158c2ecf20Sopenharmony_ci#define EMAC_TPD_TSTAMP_SAVE	0x80000000
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* EMAC Errors in emac_rrd.word[3] */
2188c2ecf20Sopenharmony_ci#define EMAC_RRD_L4F		BIT(14)
2198c2ecf20Sopenharmony_ci#define EMAC_RRD_IPF		BIT(15)
2208c2ecf20Sopenharmony_ci#define EMAC_RRD_CRC		BIT(21)
2218c2ecf20Sopenharmony_ci#define EMAC_RRD_FAE		BIT(22)
2228c2ecf20Sopenharmony_ci#define EMAC_RRD_TRN		BIT(23)
2238c2ecf20Sopenharmony_ci#define EMAC_RRD_RNT		BIT(24)
2248c2ecf20Sopenharmony_ci#define EMAC_RRD_INC		BIT(25)
2258c2ecf20Sopenharmony_ci#define EMAC_RRD_FOV		BIT(29)
2268c2ecf20Sopenharmony_ci#define EMAC_RRD_LEN		BIT(30)
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/* Error bits that will result in a received frame being discarded */
2298c2ecf20Sopenharmony_ci#define EMAC_RRD_ERROR (EMAC_RRD_IPF | EMAC_RRD_CRC | EMAC_RRD_FAE | \
2308c2ecf20Sopenharmony_ci			EMAC_RRD_TRN | EMAC_RRD_RNT | EMAC_RRD_INC | \
2318c2ecf20Sopenharmony_ci			EMAC_RRD_FOV | EMAC_RRD_LEN)
2328c2ecf20Sopenharmony_ci#define EMAC_RRD_STATS_DW_IDX 3
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci#define EMAC_RRD(RXQ, SIZE, IDX)	((RXQ)->rrd.v_addr + (SIZE * (IDX)))
2358c2ecf20Sopenharmony_ci#define EMAC_RFD(RXQ, SIZE, IDX)	((RXQ)->rfd.v_addr + (SIZE * (IDX)))
2368c2ecf20Sopenharmony_ci#define EMAC_TPD(TXQ, SIZE, IDX)	((TXQ)->tpd.v_addr + (SIZE * (IDX)))
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci#define GET_RFD_BUFFER(RXQ, IDX)	(&((RXQ)->rfd.rfbuff[(IDX)]))
2398c2ecf20Sopenharmony_ci#define GET_TPD_BUFFER(RTQ, IDX)	(&((RTQ)->tpd.tpbuff[(IDX)]))
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci#define EMAC_TX_POLL_HWTXTSTAMP_THRESHOLD	8
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define ISR_RX_PKT      (\
2448c2ecf20Sopenharmony_ci	RX_PKT_INT0     |\
2458c2ecf20Sopenharmony_ci	RX_PKT_INT1     |\
2468c2ecf20Sopenharmony_ci	RX_PKT_INT2     |\
2478c2ecf20Sopenharmony_ci	RX_PKT_INT3)
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_civoid emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	u32 crc32, bit, reg, mta;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* Calculate the CRC of the MAC address */
2548c2ecf20Sopenharmony_ci	crc32 = ether_crc(ETH_ALEN, addr);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* The HASH Table is an array of 2 32-bit registers. It is
2578c2ecf20Sopenharmony_ci	 * treated like an array of 64 bits (BitArray[hash_value]).
2588c2ecf20Sopenharmony_ci	 * Use the upper 6 bits of the above CRC as the hash value.
2598c2ecf20Sopenharmony_ci	 */
2608c2ecf20Sopenharmony_ci	reg = (crc32 >> 31) & 0x1;
2618c2ecf20Sopenharmony_ci	bit = (crc32 >> 26) & 0x1F;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	mta = readl(adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
2648c2ecf20Sopenharmony_ci	mta |= BIT(bit);
2658c2ecf20Sopenharmony_ci	writel(mta, adpt->base + EMAC_HASH_TAB_REG0 + (reg << 2));
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_civoid emac_mac_multicast_addr_clear(struct emac_adapter *adpt)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	writel(0, adpt->base + EMAC_HASH_TAB_REG0);
2718c2ecf20Sopenharmony_ci	writel(0, adpt->base + EMAC_HASH_TAB_REG1);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/* definitions for RSS */
2758c2ecf20Sopenharmony_ci#define EMAC_RSS_KEY(_i, _type) \
2768c2ecf20Sopenharmony_ci		(EMAC_RSS_KEY0 + ((_i) * sizeof(_type)))
2778c2ecf20Sopenharmony_ci#define EMAC_RSS_TBL(_i, _type) \
2788c2ecf20Sopenharmony_ci		(EMAC_IDT_TABLE0 + ((_i) * sizeof(_type)))
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* Config MAC modes */
2818c2ecf20Sopenharmony_civoid emac_mac_mode_config(struct emac_adapter *adpt)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct net_device *netdev = adpt->netdev;
2848c2ecf20Sopenharmony_ci	u32 mac;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	mac = readl(adpt->base + EMAC_MAC_CTRL);
2878c2ecf20Sopenharmony_ci	mac &= ~(VLAN_STRIP | PROM_MODE | MULTI_ALL | MAC_LP_EN);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2908c2ecf20Sopenharmony_ci		mac |= VLAN_STRIP;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (netdev->flags & IFF_PROMISC)
2938c2ecf20Sopenharmony_ci		mac |= PROM_MODE;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (netdev->flags & IFF_ALLMULTI)
2968c2ecf20Sopenharmony_ci		mac |= MULTI_ALL;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	writel(mac, adpt->base + EMAC_MAC_CTRL);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/* Config descriptor rings */
3028c2ecf20Sopenharmony_cistatic void emac_mac_dma_rings_config(struct emac_adapter *adpt)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	/* TPD (Transmit Packet Descriptor) */
3058c2ecf20Sopenharmony_ci	writel(upper_32_bits(adpt->tx_q.tpd.dma_addr),
3068c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_1);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	writel(lower_32_bits(adpt->tx_q.tpd.dma_addr),
3098c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_8);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	writel(adpt->tx_q.tpd.count & TPD_RING_SIZE_BMSK,
3128c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_9);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* RFD (Receive Free Descriptor) & RRD (Receive Return Descriptor) */
3158c2ecf20Sopenharmony_ci	writel(upper_32_bits(adpt->rx_q.rfd.dma_addr),
3168c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_0);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	writel(lower_32_bits(adpt->rx_q.rfd.dma_addr),
3198c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_2);
3208c2ecf20Sopenharmony_ci	writel(lower_32_bits(adpt->rx_q.rrd.dma_addr),
3218c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_5);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	writel(adpt->rx_q.rfd.count & RFD_RING_SIZE_BMSK,
3248c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_3);
3258c2ecf20Sopenharmony_ci	writel(adpt->rx_q.rrd.count & RRD_RING_SIZE_BMSK,
3268c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_6);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	writel(adpt->rxbuf_size & RX_BUFFER_SIZE_BMSK,
3298c2ecf20Sopenharmony_ci	       adpt->base + EMAC_DESC_CTRL_4);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	writel(0, adpt->base + EMAC_DESC_CTRL_11);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Load all of the base addresses above and ensure that triggering HW to
3348c2ecf20Sopenharmony_ci	 * read ring pointers is flushed
3358c2ecf20Sopenharmony_ci	 */
3368c2ecf20Sopenharmony_ci	writel(1, adpt->base + EMAC_INTER_SRAM_PART9);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci/* Config transmit parameters */
3408c2ecf20Sopenharmony_cistatic void emac_mac_tx_config(struct emac_adapter *adpt)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	u32 val;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	writel((EMAC_MAX_TX_OFFLOAD_THRESH >> 3) &
3458c2ecf20Sopenharmony_ci	       JUMBO_TASK_OFFLOAD_THRESHOLD_BMSK, adpt->base + EMAC_TXQ_CTRL_1);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	val = (adpt->tpd_burst << NUM_TPD_BURST_PREF_SHFT) &
3488c2ecf20Sopenharmony_ci	       NUM_TPD_BURST_PREF_BMSK;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	val |= TXQ_MODE | LS_8023_SP;
3518c2ecf20Sopenharmony_ci	val |= (0x0100 << NUM_TXF_BURST_PREF_SHFT) &
3528c2ecf20Sopenharmony_ci		NUM_TXF_BURST_PREF_BMSK;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_TXQ_CTRL_0);
3558c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_2,
3568c2ecf20Sopenharmony_ci			  (TXF_HWM_BMSK | TXF_LWM_BMSK), 0);
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/* Config receive parameters */
3608c2ecf20Sopenharmony_cistatic void emac_mac_rx_config(struct emac_adapter *adpt)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	u32 val;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	val = (adpt->rfd_burst << NUM_RFD_BURST_PREF_SHFT) &
3658c2ecf20Sopenharmony_ci	       NUM_RFD_BURST_PREF_BMSK;
3668c2ecf20Sopenharmony_ci	val |= (SP_IPV6 | CUT_THRU_EN);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_RXQ_CTRL_0);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	val = readl(adpt->base + EMAC_RXQ_CTRL_1);
3718c2ecf20Sopenharmony_ci	val &= ~(JUMBO_1KAH_BMSK | RFD_PREF_LOW_THRESHOLD_BMSK |
3728c2ecf20Sopenharmony_ci		 RFD_PREF_UP_THRESHOLD_BMSK);
3738c2ecf20Sopenharmony_ci	val |= (JUMBO_1KAH << JUMBO_1KAH_SHFT) |
3748c2ecf20Sopenharmony_ci		(RFD_PREF_LOW_TH << RFD_PREF_LOW_THRESHOLD_SHFT) |
3758c2ecf20Sopenharmony_ci		(RFD_PREF_UP_TH  << RFD_PREF_UP_THRESHOLD_SHFT);
3768c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_RXQ_CTRL_1);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	val = readl(adpt->base + EMAC_RXQ_CTRL_2);
3798c2ecf20Sopenharmony_ci	val &= ~(RXF_DOF_THRESHOLD_BMSK | RXF_UOF_THRESHOLD_BMSK);
3808c2ecf20Sopenharmony_ci	val |= (RXF_DOF_THRESFHOLD  << RXF_DOF_THRESHOLD_SHFT) |
3818c2ecf20Sopenharmony_ci		(RXF_UOF_THRESFHOLD << RXF_UOF_THRESHOLD_SHFT);
3828c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_RXQ_CTRL_2);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	val = readl(adpt->base + EMAC_RXQ_CTRL_3);
3858c2ecf20Sopenharmony_ci	val &= ~(RXD_TIMER_BMSK | RXD_THRESHOLD_BMSK);
3868c2ecf20Sopenharmony_ci	val |= RXD_TH << RXD_THRESHOLD_SHFT;
3878c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_RXQ_CTRL_3);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci/* Config dma */
3918c2ecf20Sopenharmony_cistatic void emac_mac_dma_config(struct emac_adapter *adpt)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	u32 dma_ctrl = DMAR_REQ_PRI;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	switch (adpt->dma_order) {
3968c2ecf20Sopenharmony_ci	case emac_dma_ord_in:
3978c2ecf20Sopenharmony_ci		dma_ctrl |= IN_ORDER_MODE;
3988c2ecf20Sopenharmony_ci		break;
3998c2ecf20Sopenharmony_ci	case emac_dma_ord_enh:
4008c2ecf20Sopenharmony_ci		dma_ctrl |= ENH_ORDER_MODE;
4018c2ecf20Sopenharmony_ci		break;
4028c2ecf20Sopenharmony_ci	case emac_dma_ord_out:
4038c2ecf20Sopenharmony_ci		dma_ctrl |= OUT_ORDER_MODE;
4048c2ecf20Sopenharmony_ci		break;
4058c2ecf20Sopenharmony_ci	default:
4068c2ecf20Sopenharmony_ci		break;
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	dma_ctrl |= (((u32)adpt->dmar_block) << REGRDBLEN_SHFT) &
4108c2ecf20Sopenharmony_ci						REGRDBLEN_BMSK;
4118c2ecf20Sopenharmony_ci	dma_ctrl |= (((u32)adpt->dmaw_block) << REGWRBLEN_SHFT) &
4128c2ecf20Sopenharmony_ci						REGWRBLEN_BMSK;
4138c2ecf20Sopenharmony_ci	dma_ctrl |= (((u32)adpt->dmar_dly_cnt) << DMAR_DLY_CNT_SHFT) &
4148c2ecf20Sopenharmony_ci						DMAR_DLY_CNT_BMSK;
4158c2ecf20Sopenharmony_ci	dma_ctrl |= (((u32)adpt->dmaw_dly_cnt) << DMAW_DLY_CNT_SHFT) &
4168c2ecf20Sopenharmony_ci						DMAW_DLY_CNT_BMSK;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* config DMA and ensure that configuration is flushed to HW */
4198c2ecf20Sopenharmony_ci	writel(dma_ctrl, adpt->base + EMAC_DMA_CTRL);
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci/* set MAC address */
4238c2ecf20Sopenharmony_cistatic void emac_set_mac_address(struct emac_adapter *adpt, u8 *addr)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	u32 sta;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* for example: 00-A0-C6-11-22-33
4288c2ecf20Sopenharmony_ci	 * 0<-->C6112233, 1<-->00A0.
4298c2ecf20Sopenharmony_ci	 */
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* low 32bit word */
4328c2ecf20Sopenharmony_ci	sta = (((u32)addr[2]) << 24) | (((u32)addr[3]) << 16) |
4338c2ecf20Sopenharmony_ci	      (((u32)addr[4]) << 8)  | (((u32)addr[5]));
4348c2ecf20Sopenharmony_ci	writel(sta, adpt->base + EMAC_MAC_STA_ADDR0);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* hight 32bit word */
4378c2ecf20Sopenharmony_ci	sta = (((u32)addr[0]) << 8) | (u32)addr[1];
4388c2ecf20Sopenharmony_ci	writel(sta, adpt->base + EMAC_MAC_STA_ADDR1);
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic void emac_mac_config(struct emac_adapter *adpt)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	struct net_device *netdev = adpt->netdev;
4448c2ecf20Sopenharmony_ci	unsigned int max_frame;
4458c2ecf20Sopenharmony_ci	u32 val;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	emac_set_mac_address(adpt, netdev->dev_addr);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4508c2ecf20Sopenharmony_ci	adpt->rxbuf_size = netdev->mtu > EMAC_DEF_RX_BUF_SIZE ?
4518c2ecf20Sopenharmony_ci		ALIGN(max_frame, 8) : EMAC_DEF_RX_BUF_SIZE;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	emac_mac_dma_rings_config(adpt);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	writel(netdev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
4568c2ecf20Sopenharmony_ci	       adpt->base + EMAC_MAX_FRAM_LEN_CTRL);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	emac_mac_tx_config(adpt);
4598c2ecf20Sopenharmony_ci	emac_mac_rx_config(adpt);
4608c2ecf20Sopenharmony_ci	emac_mac_dma_config(adpt);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	val = readl(adpt->base + EMAC_AXI_MAST_CTRL);
4638c2ecf20Sopenharmony_ci	val &= ~(DATA_BYTE_SWAP | MAX_BOUND);
4648c2ecf20Sopenharmony_ci	val |= MAX_BTYPE;
4658c2ecf20Sopenharmony_ci	writel(val, adpt->base + EMAC_AXI_MAST_CTRL);
4668c2ecf20Sopenharmony_ci	writel(0, adpt->base + EMAC_CLK_GATE_CTRL);
4678c2ecf20Sopenharmony_ci	writel(RX_UNCPL_INT_EN, adpt->base + EMAC_MISC_CTRL);
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_civoid emac_mac_reset(struct emac_adapter *adpt)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	emac_mac_stop(adpt);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, SOFT_RST);
4758c2ecf20Sopenharmony_ci	usleep_range(100, 150); /* reset may take up to 100usec */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* interrupt clear-on-read */
4788c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_DMA_MAS_CTRL, 0, INT_RD_CLR_EN);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic void emac_mac_start(struct emac_adapter *adpt)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct phy_device *phydev = adpt->phydev;
4848c2ecf20Sopenharmony_ci	u32 mac, csr1;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	/* enable tx queue */
4878c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, 0, TXQ_EN);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* enable rx queue */
4908c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, 0, RXQ_EN);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* enable mac control */
4938c2ecf20Sopenharmony_ci	mac = readl(adpt->base + EMAC_MAC_CTRL);
4948c2ecf20Sopenharmony_ci	csr1 = readl(adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	mac |= TXEN | RXEN;     /* enable RX/TX */
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	/* Configure MAC flow control. If set to automatic, then match
4998c2ecf20Sopenharmony_ci	 * whatever the PHY does. Otherwise, enable or disable it, depending
5008c2ecf20Sopenharmony_ci	 * on what the user configured via ethtool.
5018c2ecf20Sopenharmony_ci	 */
5028c2ecf20Sopenharmony_ci	mac &= ~(RXFC | TXFC);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (adpt->automatic) {
5058c2ecf20Sopenharmony_ci		/* If it's set to automatic, then update our local values */
5068c2ecf20Sopenharmony_ci		adpt->rx_flow_control = phydev->pause;
5078c2ecf20Sopenharmony_ci		adpt->tx_flow_control = phydev->pause != phydev->asym_pause;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci	mac |= adpt->rx_flow_control ? RXFC : 0;
5108c2ecf20Sopenharmony_ci	mac |= adpt->tx_flow_control ? TXFC : 0;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	/* setup link speed */
5138c2ecf20Sopenharmony_ci	mac &= ~SPEED_MASK;
5148c2ecf20Sopenharmony_ci	if (phydev->speed == SPEED_1000) {
5158c2ecf20Sopenharmony_ci		mac |= SPEED(2);
5168c2ecf20Sopenharmony_ci		csr1 |= FREQ_MODE;
5178c2ecf20Sopenharmony_ci	} else {
5188c2ecf20Sopenharmony_ci		mac |= SPEED(1);
5198c2ecf20Sopenharmony_ci		csr1 &= ~FREQ_MODE;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	if (phydev->duplex == DUPLEX_FULL)
5238c2ecf20Sopenharmony_ci		mac |= FULLD;
5248c2ecf20Sopenharmony_ci	else
5258c2ecf20Sopenharmony_ci		mac &= ~FULLD;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* other parameters */
5288c2ecf20Sopenharmony_ci	mac |= (CRCE | PCRCE);
5298c2ecf20Sopenharmony_ci	mac |= ((adpt->preamble << PRLEN_SHFT) & PRLEN_BMSK);
5308c2ecf20Sopenharmony_ci	mac |= BROAD_EN;
5318c2ecf20Sopenharmony_ci	mac |= FLCHK;
5328c2ecf20Sopenharmony_ci	mac &= ~RX_CHKSUM_EN;
5338c2ecf20Sopenharmony_ci	mac &= ~(HUGEN | VLAN_STRIP | TPAUSE | SIMR | HUGE | MULTI_ALL |
5348c2ecf20Sopenharmony_ci		 DEBUG_MODE | SINGLE_PAUSE_MODE);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* Enable single-pause-frame mode if requested.
5378c2ecf20Sopenharmony_ci	 *
5388c2ecf20Sopenharmony_ci	 * If enabled, the EMAC will send a single pause frame when the RX
5398c2ecf20Sopenharmony_ci	 * queue is full.  This normally leads to packet loss because
5408c2ecf20Sopenharmony_ci	 * the pause frame disables the remote MAC only for 33ms (the quanta),
5418c2ecf20Sopenharmony_ci	 * and then the remote MAC continues sending packets even though
5428c2ecf20Sopenharmony_ci	 * the RX queue is still full.
5438c2ecf20Sopenharmony_ci	 *
5448c2ecf20Sopenharmony_ci	 * If disabled, the EMAC sends a pause frame every 31ms until the RX
5458c2ecf20Sopenharmony_ci	 * queue is no longer full.  Normally, this is the preferred
5468c2ecf20Sopenharmony_ci	 * method of operation.  However, when the system is hung (e.g.
5478c2ecf20Sopenharmony_ci	 * cores are halted), the EMAC interrupt handler is never called
5488c2ecf20Sopenharmony_ci	 * and so the RX queue fills up quickly and stays full.  The resuling
5498c2ecf20Sopenharmony_ci	 * non-stop "flood" of pause frames sometimes has the effect of
5508c2ecf20Sopenharmony_ci	 * disabling nearby switches.  In some cases, other nearby switches
5518c2ecf20Sopenharmony_ci	 * are also affected, shutting down the entire network.
5528c2ecf20Sopenharmony_ci	 *
5538c2ecf20Sopenharmony_ci	 * The user can enable or disable single-pause-frame mode
5548c2ecf20Sopenharmony_ci	 * via ethtool.
5558c2ecf20Sopenharmony_ci	 */
5568c2ecf20Sopenharmony_ci	mac |= adpt->single_pause_mode ? SINGLE_PAUSE_MODE : 0;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	writel_relaxed(csr1, adpt->csr + EMAC_EMAC_WRAPPER_CSR1);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	writel_relaxed(mac, adpt->base + EMAC_MAC_CTRL);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* enable interrupt read clear, low power sleep mode and
5638c2ecf20Sopenharmony_ci	 * the irq moderators
5648c2ecf20Sopenharmony_ci	 */
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	writel_relaxed(adpt->irq_mod, adpt->base + EMAC_IRQ_MOD_TIM_INIT);
5678c2ecf20Sopenharmony_ci	writel_relaxed(INT_RD_CLR_EN | LPW_MODE | IRQ_MODERATOR_EN |
5688c2ecf20Sopenharmony_ci			IRQ_MODERATOR2_EN, adpt->base + EMAC_DMA_MAS_CTRL);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	emac_mac_mode_config(adpt);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_ATHR_HEADER_CTRL,
5738c2ecf20Sopenharmony_ci			  (HEADER_ENABLE | HEADER_CNT_EN), 0);
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_civoid emac_mac_stop(struct emac_adapter *adpt)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_RXQ_CTRL_0, RXQ_EN, 0);
5798c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_TXQ_CTRL_0, TXQ_EN, 0);
5808c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + EMAC_MAC_CTRL, TXEN | RXEN, 0);
5818c2ecf20Sopenharmony_ci	usleep_range(1000, 1050); /* stopping mac may take upto 1msec */
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci/* Free all descriptors of given transmit queue */
5858c2ecf20Sopenharmony_cistatic void emac_tx_q_descs_free(struct emac_adapter *adpt)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct emac_tx_queue *tx_q = &adpt->tx_q;
5888c2ecf20Sopenharmony_ci	unsigned int i;
5898c2ecf20Sopenharmony_ci	size_t size;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* ring already cleared, nothing to do */
5928c2ecf20Sopenharmony_ci	if (!tx_q->tpd.tpbuff)
5938c2ecf20Sopenharmony_ci		return;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	for (i = 0; i < tx_q->tpd.count; i++) {
5968c2ecf20Sopenharmony_ci		struct emac_buffer *tpbuf = GET_TPD_BUFFER(tx_q, i);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		if (tpbuf->dma_addr) {
5998c2ecf20Sopenharmony_ci			dma_unmap_single(adpt->netdev->dev.parent,
6008c2ecf20Sopenharmony_ci					 tpbuf->dma_addr, tpbuf->length,
6018c2ecf20Sopenharmony_ci					 DMA_TO_DEVICE);
6028c2ecf20Sopenharmony_ci			tpbuf->dma_addr = 0;
6038c2ecf20Sopenharmony_ci		}
6048c2ecf20Sopenharmony_ci		if (tpbuf->skb) {
6058c2ecf20Sopenharmony_ci			dev_kfree_skb_any(tpbuf->skb);
6068c2ecf20Sopenharmony_ci			tpbuf->skb = NULL;
6078c2ecf20Sopenharmony_ci		}
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	size = sizeof(struct emac_buffer) * tx_q->tpd.count;
6118c2ecf20Sopenharmony_ci	memset(tx_q->tpd.tpbuff, 0, size);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/* clear the descriptor ring */
6148c2ecf20Sopenharmony_ci	memset(tx_q->tpd.v_addr, 0, tx_q->tpd.size);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	tx_q->tpd.consume_idx = 0;
6178c2ecf20Sopenharmony_ci	tx_q->tpd.produce_idx = 0;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/* Free all descriptors of given receive queue */
6218c2ecf20Sopenharmony_cistatic void emac_rx_q_free_descs(struct emac_adapter *adpt)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct device *dev = adpt->netdev->dev.parent;
6248c2ecf20Sopenharmony_ci	struct emac_rx_queue *rx_q = &adpt->rx_q;
6258c2ecf20Sopenharmony_ci	unsigned int i;
6268c2ecf20Sopenharmony_ci	size_t size;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	/* ring already cleared, nothing to do */
6298c2ecf20Sopenharmony_ci	if (!rx_q->rfd.rfbuff)
6308c2ecf20Sopenharmony_ci		return;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	for (i = 0; i < rx_q->rfd.count; i++) {
6338c2ecf20Sopenharmony_ci		struct emac_buffer *rfbuf = GET_RFD_BUFFER(rx_q, i);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (rfbuf->dma_addr) {
6368c2ecf20Sopenharmony_ci			dma_unmap_single(dev, rfbuf->dma_addr, rfbuf->length,
6378c2ecf20Sopenharmony_ci					 DMA_FROM_DEVICE);
6388c2ecf20Sopenharmony_ci			rfbuf->dma_addr = 0;
6398c2ecf20Sopenharmony_ci		}
6408c2ecf20Sopenharmony_ci		if (rfbuf->skb) {
6418c2ecf20Sopenharmony_ci			dev_kfree_skb(rfbuf->skb);
6428c2ecf20Sopenharmony_ci			rfbuf->skb = NULL;
6438c2ecf20Sopenharmony_ci		}
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	size =  sizeof(struct emac_buffer) * rx_q->rfd.count;
6478c2ecf20Sopenharmony_ci	memset(rx_q->rfd.rfbuff, 0, size);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* clear the descriptor rings */
6508c2ecf20Sopenharmony_ci	memset(rx_q->rrd.v_addr, 0, rx_q->rrd.size);
6518c2ecf20Sopenharmony_ci	rx_q->rrd.produce_idx = 0;
6528c2ecf20Sopenharmony_ci	rx_q->rrd.consume_idx = 0;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	memset(rx_q->rfd.v_addr, 0, rx_q->rfd.size);
6558c2ecf20Sopenharmony_ci	rx_q->rfd.produce_idx = 0;
6568c2ecf20Sopenharmony_ci	rx_q->rfd.consume_idx = 0;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci/* Free all buffers associated with given transmit queue */
6608c2ecf20Sopenharmony_cistatic void emac_tx_q_bufs_free(struct emac_adapter *adpt)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	struct emac_tx_queue *tx_q = &adpt->tx_q;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	emac_tx_q_descs_free(adpt);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	kfree(tx_q->tpd.tpbuff);
6678c2ecf20Sopenharmony_ci	tx_q->tpd.tpbuff = NULL;
6688c2ecf20Sopenharmony_ci	tx_q->tpd.v_addr = NULL;
6698c2ecf20Sopenharmony_ci	tx_q->tpd.dma_addr = 0;
6708c2ecf20Sopenharmony_ci	tx_q->tpd.size = 0;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci/* Allocate TX descriptor ring for the given transmit queue */
6748c2ecf20Sopenharmony_cistatic int emac_tx_q_desc_alloc(struct emac_adapter *adpt,
6758c2ecf20Sopenharmony_ci				struct emac_tx_queue *tx_q)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	struct emac_ring_header *ring_header = &adpt->ring_header;
6788c2ecf20Sopenharmony_ci	int node = dev_to_node(adpt->netdev->dev.parent);
6798c2ecf20Sopenharmony_ci	size_t size;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	size = sizeof(struct emac_buffer) * tx_q->tpd.count;
6828c2ecf20Sopenharmony_ci	tx_q->tpd.tpbuff = kzalloc_node(size, GFP_KERNEL, node);
6838c2ecf20Sopenharmony_ci	if (!tx_q->tpd.tpbuff)
6848c2ecf20Sopenharmony_ci		return -ENOMEM;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	tx_q->tpd.size = tx_q->tpd.count * (adpt->tpd_size * 4);
6878c2ecf20Sopenharmony_ci	tx_q->tpd.dma_addr = ring_header->dma_addr + ring_header->used;
6888c2ecf20Sopenharmony_ci	tx_q->tpd.v_addr = ring_header->v_addr + ring_header->used;
6898c2ecf20Sopenharmony_ci	ring_header->used += ALIGN(tx_q->tpd.size, 8);
6908c2ecf20Sopenharmony_ci	tx_q->tpd.produce_idx = 0;
6918c2ecf20Sopenharmony_ci	tx_q->tpd.consume_idx = 0;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return 0;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci/* Free all buffers associated with given transmit queue */
6978c2ecf20Sopenharmony_cistatic void emac_rx_q_bufs_free(struct emac_adapter *adpt)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct emac_rx_queue *rx_q = &adpt->rx_q;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	emac_rx_q_free_descs(adpt);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	kfree(rx_q->rfd.rfbuff);
7048c2ecf20Sopenharmony_ci	rx_q->rfd.rfbuff   = NULL;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	rx_q->rfd.v_addr   = NULL;
7078c2ecf20Sopenharmony_ci	rx_q->rfd.dma_addr = 0;
7088c2ecf20Sopenharmony_ci	rx_q->rfd.size     = 0;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	rx_q->rrd.v_addr   = NULL;
7118c2ecf20Sopenharmony_ci	rx_q->rrd.dma_addr = 0;
7128c2ecf20Sopenharmony_ci	rx_q->rrd.size     = 0;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci/* Allocate RX descriptor rings for the given receive queue */
7168c2ecf20Sopenharmony_cistatic int emac_rx_descs_alloc(struct emac_adapter *adpt)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	struct emac_ring_header *ring_header = &adpt->ring_header;
7198c2ecf20Sopenharmony_ci	int node = dev_to_node(adpt->netdev->dev.parent);
7208c2ecf20Sopenharmony_ci	struct emac_rx_queue *rx_q = &adpt->rx_q;
7218c2ecf20Sopenharmony_ci	size_t size;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	size = sizeof(struct emac_buffer) * rx_q->rfd.count;
7248c2ecf20Sopenharmony_ci	rx_q->rfd.rfbuff = kzalloc_node(size, GFP_KERNEL, node);
7258c2ecf20Sopenharmony_ci	if (!rx_q->rfd.rfbuff)
7268c2ecf20Sopenharmony_ci		return -ENOMEM;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	rx_q->rrd.size = rx_q->rrd.count * (adpt->rrd_size * 4);
7298c2ecf20Sopenharmony_ci	rx_q->rfd.size = rx_q->rfd.count * (adpt->rfd_size * 4);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	rx_q->rrd.dma_addr = ring_header->dma_addr + ring_header->used;
7328c2ecf20Sopenharmony_ci	rx_q->rrd.v_addr   = ring_header->v_addr + ring_header->used;
7338c2ecf20Sopenharmony_ci	ring_header->used += ALIGN(rx_q->rrd.size, 8);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	rx_q->rfd.dma_addr = ring_header->dma_addr + ring_header->used;
7368c2ecf20Sopenharmony_ci	rx_q->rfd.v_addr   = ring_header->v_addr + ring_header->used;
7378c2ecf20Sopenharmony_ci	ring_header->used += ALIGN(rx_q->rfd.size, 8);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	rx_q->rrd.produce_idx = 0;
7408c2ecf20Sopenharmony_ci	rx_q->rrd.consume_idx = 0;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	rx_q->rfd.produce_idx = 0;
7438c2ecf20Sopenharmony_ci	rx_q->rfd.consume_idx = 0;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	return 0;
7468c2ecf20Sopenharmony_ci}
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci/* Allocate all TX and RX descriptor rings */
7498c2ecf20Sopenharmony_ciint emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	struct emac_ring_header *ring_header = &adpt->ring_header;
7528c2ecf20Sopenharmony_ci	struct device *dev = adpt->netdev->dev.parent;
7538c2ecf20Sopenharmony_ci	unsigned int num_tx_descs = adpt->tx_desc_cnt;
7548c2ecf20Sopenharmony_ci	unsigned int num_rx_descs = adpt->rx_desc_cnt;
7558c2ecf20Sopenharmony_ci	int ret;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	adpt->tx_q.tpd.count = adpt->tx_desc_cnt;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	adpt->rx_q.rrd.count = adpt->rx_desc_cnt;
7608c2ecf20Sopenharmony_ci	adpt->rx_q.rfd.count = adpt->rx_desc_cnt;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	/* Ring DMA buffer. Each ring may need up to 8 bytes for alignment,
7638c2ecf20Sopenharmony_ci	 * hence the additional padding bytes are allocated.
7648c2ecf20Sopenharmony_ci	 */
7658c2ecf20Sopenharmony_ci	ring_header->size = num_tx_descs * (adpt->tpd_size * 4) +
7668c2ecf20Sopenharmony_ci			    num_rx_descs * (adpt->rfd_size * 4) +
7678c2ecf20Sopenharmony_ci			    num_rx_descs * (adpt->rrd_size * 4) +
7688c2ecf20Sopenharmony_ci			    8 + 2 * 8; /* 8 byte per one Tx and two Rx rings */
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	ring_header->used = 0;
7718c2ecf20Sopenharmony_ci	ring_header->v_addr = dma_alloc_coherent(dev, ring_header->size,
7728c2ecf20Sopenharmony_ci						 &ring_header->dma_addr,
7738c2ecf20Sopenharmony_ci						 GFP_KERNEL);
7748c2ecf20Sopenharmony_ci	if (!ring_header->v_addr)
7758c2ecf20Sopenharmony_ci		return -ENOMEM;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	ring_header->used = ALIGN(ring_header->dma_addr, 8) -
7788c2ecf20Sopenharmony_ci							ring_header->dma_addr;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	ret = emac_tx_q_desc_alloc(adpt, &adpt->tx_q);
7818c2ecf20Sopenharmony_ci	if (ret) {
7828c2ecf20Sopenharmony_ci		netdev_err(adpt->netdev, "error: Tx Queue alloc failed\n");
7838c2ecf20Sopenharmony_ci		goto err_alloc_tx;
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	ret = emac_rx_descs_alloc(adpt);
7878c2ecf20Sopenharmony_ci	if (ret) {
7888c2ecf20Sopenharmony_ci		netdev_err(adpt->netdev, "error: Rx Queue alloc failed\n");
7898c2ecf20Sopenharmony_ci		goto err_alloc_rx;
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	return 0;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_cierr_alloc_rx:
7958c2ecf20Sopenharmony_ci	emac_tx_q_bufs_free(adpt);
7968c2ecf20Sopenharmony_cierr_alloc_tx:
7978c2ecf20Sopenharmony_ci	dma_free_coherent(dev, ring_header->size,
7988c2ecf20Sopenharmony_ci			  ring_header->v_addr, ring_header->dma_addr);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	ring_header->v_addr   = NULL;
8018c2ecf20Sopenharmony_ci	ring_header->dma_addr = 0;
8028c2ecf20Sopenharmony_ci	ring_header->size     = 0;
8038c2ecf20Sopenharmony_ci	ring_header->used     = 0;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	return ret;
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci/* Free all TX and RX descriptor rings */
8098c2ecf20Sopenharmony_civoid emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt)
8108c2ecf20Sopenharmony_ci{
8118c2ecf20Sopenharmony_ci	struct emac_ring_header *ring_header = &adpt->ring_header;
8128c2ecf20Sopenharmony_ci	struct device *dev = adpt->netdev->dev.parent;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	emac_tx_q_bufs_free(adpt);
8158c2ecf20Sopenharmony_ci	emac_rx_q_bufs_free(adpt);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	dma_free_coherent(dev, ring_header->size,
8188c2ecf20Sopenharmony_ci			  ring_header->v_addr, ring_header->dma_addr);
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	ring_header->v_addr   = NULL;
8218c2ecf20Sopenharmony_ci	ring_header->dma_addr = 0;
8228c2ecf20Sopenharmony_ci	ring_header->size     = 0;
8238c2ecf20Sopenharmony_ci	ring_header->used     = 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci/* Initialize descriptor rings */
8278c2ecf20Sopenharmony_cistatic void emac_mac_rx_tx_ring_reset_all(struct emac_adapter *adpt)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	unsigned int i;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	adpt->tx_q.tpd.produce_idx = 0;
8328c2ecf20Sopenharmony_ci	adpt->tx_q.tpd.consume_idx = 0;
8338c2ecf20Sopenharmony_ci	for (i = 0; i < adpt->tx_q.tpd.count; i++)
8348c2ecf20Sopenharmony_ci		adpt->tx_q.tpd.tpbuff[i].dma_addr = 0;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	adpt->rx_q.rrd.produce_idx = 0;
8378c2ecf20Sopenharmony_ci	adpt->rx_q.rrd.consume_idx = 0;
8388c2ecf20Sopenharmony_ci	adpt->rx_q.rfd.produce_idx = 0;
8398c2ecf20Sopenharmony_ci	adpt->rx_q.rfd.consume_idx = 0;
8408c2ecf20Sopenharmony_ci	for (i = 0; i < adpt->rx_q.rfd.count; i++)
8418c2ecf20Sopenharmony_ci		adpt->rx_q.rfd.rfbuff[i].dma_addr = 0;
8428c2ecf20Sopenharmony_ci}
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci/* Produce new receive free descriptor */
8458c2ecf20Sopenharmony_cistatic void emac_mac_rx_rfd_create(struct emac_adapter *adpt,
8468c2ecf20Sopenharmony_ci				   struct emac_rx_queue *rx_q,
8478c2ecf20Sopenharmony_ci				   dma_addr_t addr)
8488c2ecf20Sopenharmony_ci{
8498c2ecf20Sopenharmony_ci	u32 *hw_rfd = EMAC_RFD(rx_q, adpt->rfd_size, rx_q->rfd.produce_idx);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	*(hw_rfd++) = lower_32_bits(addr);
8528c2ecf20Sopenharmony_ci	*hw_rfd = upper_32_bits(addr);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	if (++rx_q->rfd.produce_idx == rx_q->rfd.count)
8558c2ecf20Sopenharmony_ci		rx_q->rfd.produce_idx = 0;
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci/* Fill up receive queue's RFD with preallocated receive buffers */
8598c2ecf20Sopenharmony_cistatic void emac_mac_rx_descs_refill(struct emac_adapter *adpt,
8608c2ecf20Sopenharmony_ci				    struct emac_rx_queue *rx_q)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct emac_buffer *curr_rxbuf;
8638c2ecf20Sopenharmony_ci	struct emac_buffer *next_rxbuf;
8648c2ecf20Sopenharmony_ci	unsigned int count = 0;
8658c2ecf20Sopenharmony_ci	u32 next_produce_idx;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	next_produce_idx = rx_q->rfd.produce_idx + 1;
8688c2ecf20Sopenharmony_ci	if (next_produce_idx == rx_q->rfd.count)
8698c2ecf20Sopenharmony_ci		next_produce_idx = 0;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
8728c2ecf20Sopenharmony_ci	next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	/* this always has a blank rx_buffer*/
8758c2ecf20Sopenharmony_ci	while (!next_rxbuf->dma_addr) {
8768c2ecf20Sopenharmony_ci		struct sk_buff *skb;
8778c2ecf20Sopenharmony_ci		int ret;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		skb = netdev_alloc_skb_ip_align(adpt->netdev, adpt->rxbuf_size);
8808c2ecf20Sopenharmony_ci		if (!skb)
8818c2ecf20Sopenharmony_ci			break;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci		curr_rxbuf->dma_addr =
8848c2ecf20Sopenharmony_ci			dma_map_single(adpt->netdev->dev.parent, skb->data,
8858c2ecf20Sopenharmony_ci				       adpt->rxbuf_size, DMA_FROM_DEVICE);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci		ret = dma_mapping_error(adpt->netdev->dev.parent,
8888c2ecf20Sopenharmony_ci					curr_rxbuf->dma_addr);
8898c2ecf20Sopenharmony_ci		if (ret) {
8908c2ecf20Sopenharmony_ci			dev_kfree_skb(skb);
8918c2ecf20Sopenharmony_ci			break;
8928c2ecf20Sopenharmony_ci		}
8938c2ecf20Sopenharmony_ci		curr_rxbuf->skb = skb;
8948c2ecf20Sopenharmony_ci		curr_rxbuf->length = adpt->rxbuf_size;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci		emac_mac_rx_rfd_create(adpt, rx_q, curr_rxbuf->dma_addr);
8978c2ecf20Sopenharmony_ci		next_produce_idx = rx_q->rfd.produce_idx + 1;
8988c2ecf20Sopenharmony_ci		if (next_produce_idx == rx_q->rfd.count)
8998c2ecf20Sopenharmony_ci			next_produce_idx = 0;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci		curr_rxbuf = GET_RFD_BUFFER(rx_q, rx_q->rfd.produce_idx);
9028c2ecf20Sopenharmony_ci		next_rxbuf = GET_RFD_BUFFER(rx_q, next_produce_idx);
9038c2ecf20Sopenharmony_ci		count++;
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	if (count) {
9078c2ecf20Sopenharmony_ci		u32 prod_idx = (rx_q->rfd.produce_idx << rx_q->produce_shift) &
9088c2ecf20Sopenharmony_ci				rx_q->produce_mask;
9098c2ecf20Sopenharmony_ci		emac_reg_update32(adpt->base + rx_q->produce_reg,
9108c2ecf20Sopenharmony_ci				  rx_q->produce_mask, prod_idx);
9118c2ecf20Sopenharmony_ci	}
9128c2ecf20Sopenharmony_ci}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic void emac_adjust_link(struct net_device *netdev)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	struct emac_adapter *adpt = netdev_priv(netdev);
9178c2ecf20Sopenharmony_ci	struct phy_device *phydev = netdev->phydev;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	if (phydev->link) {
9208c2ecf20Sopenharmony_ci		emac_mac_start(adpt);
9218c2ecf20Sopenharmony_ci		emac_sgmii_link_change(adpt, true);
9228c2ecf20Sopenharmony_ci	} else {
9238c2ecf20Sopenharmony_ci		emac_sgmii_link_change(adpt, false);
9248c2ecf20Sopenharmony_ci		emac_mac_stop(adpt);
9258c2ecf20Sopenharmony_ci	}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	phy_print_status(phydev);
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci/* Bringup the interface/HW */
9318c2ecf20Sopenharmony_ciint emac_mac_up(struct emac_adapter *adpt)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct net_device *netdev = adpt->netdev;
9348c2ecf20Sopenharmony_ci	int ret;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	emac_mac_rx_tx_ring_reset_all(adpt);
9378c2ecf20Sopenharmony_ci	emac_mac_config(adpt);
9388c2ecf20Sopenharmony_ci	emac_mac_rx_descs_refill(adpt, &adpt->rx_q);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	adpt->phydev->irq = PHY_POLL;
9418c2ecf20Sopenharmony_ci	ret = phy_connect_direct(netdev, adpt->phydev, emac_adjust_link,
9428c2ecf20Sopenharmony_ci				 PHY_INTERFACE_MODE_SGMII);
9438c2ecf20Sopenharmony_ci	if (ret) {
9448c2ecf20Sopenharmony_ci		netdev_err(adpt->netdev, "could not connect phy\n");
9458c2ecf20Sopenharmony_ci		return ret;
9468c2ecf20Sopenharmony_ci	}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	phy_attached_print(adpt->phydev, NULL);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	/* enable mac irq */
9518c2ecf20Sopenharmony_ci	writel((u32)~DIS_INT, adpt->base + EMAC_INT_STATUS);
9528c2ecf20Sopenharmony_ci	writel(adpt->irq.mask, adpt->base + EMAC_INT_MASK);
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	phy_start(adpt->phydev);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	napi_enable(&adpt->rx_q.napi);
9578c2ecf20Sopenharmony_ci	netif_start_queue(netdev);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	return 0;
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci/* Bring down the interface/HW */
9638c2ecf20Sopenharmony_civoid emac_mac_down(struct emac_adapter *adpt)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	struct net_device *netdev = adpt->netdev;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	netif_stop_queue(netdev);
9688c2ecf20Sopenharmony_ci	napi_disable(&adpt->rx_q.napi);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	phy_stop(adpt->phydev);
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	/* Interrupts must be disabled before the PHY is disconnected, to
9738c2ecf20Sopenharmony_ci	 * avoid a race condition where adjust_link is null when we get
9748c2ecf20Sopenharmony_ci	 * an interrupt.
9758c2ecf20Sopenharmony_ci	 */
9768c2ecf20Sopenharmony_ci	writel(DIS_INT, adpt->base + EMAC_INT_STATUS);
9778c2ecf20Sopenharmony_ci	writel(0, adpt->base + EMAC_INT_MASK);
9788c2ecf20Sopenharmony_ci	synchronize_irq(adpt->irq.irq);
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	phy_disconnect(adpt->phydev);
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	emac_mac_reset(adpt);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	emac_tx_q_descs_free(adpt);
9858c2ecf20Sopenharmony_ci	netdev_reset_queue(adpt->netdev);
9868c2ecf20Sopenharmony_ci	emac_rx_q_free_descs(adpt);
9878c2ecf20Sopenharmony_ci}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci/* Consume next received packet descriptor */
9908c2ecf20Sopenharmony_cistatic bool emac_rx_process_rrd(struct emac_adapter *adpt,
9918c2ecf20Sopenharmony_ci				struct emac_rx_queue *rx_q,
9928c2ecf20Sopenharmony_ci				struct emac_rrd *rrd)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	u32 *hw_rrd = EMAC_RRD(rx_q, adpt->rrd_size, rx_q->rrd.consume_idx);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	rrd->word[3] = *(hw_rrd + 3);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	if (!RRD_UPDT(rrd))
9998c2ecf20Sopenharmony_ci		return false;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	rrd->word[4] = 0;
10028c2ecf20Sopenharmony_ci	rrd->word[5] = 0;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	rrd->word[0] = *(hw_rrd++);
10058c2ecf20Sopenharmony_ci	rrd->word[1] = *(hw_rrd++);
10068c2ecf20Sopenharmony_ci	rrd->word[2] = *(hw_rrd++);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	if (unlikely(RRD_NOR(rrd) != 1)) {
10098c2ecf20Sopenharmony_ci		netdev_err(adpt->netdev,
10108c2ecf20Sopenharmony_ci			   "error: multi-RFD not support yet! nor:%lu\n",
10118c2ecf20Sopenharmony_ci			   RRD_NOR(rrd));
10128c2ecf20Sopenharmony_ci	}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	/* mark rrd as processed */
10158c2ecf20Sopenharmony_ci	RRD_UPDT_SET(rrd, 0);
10168c2ecf20Sopenharmony_ci	*hw_rrd = rrd->word[3];
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	if (++rx_q->rrd.consume_idx == rx_q->rrd.count)
10198c2ecf20Sopenharmony_ci		rx_q->rrd.consume_idx = 0;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return true;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci/* Produce new transmit descriptor */
10258c2ecf20Sopenharmony_cistatic void emac_tx_tpd_create(struct emac_adapter *adpt,
10268c2ecf20Sopenharmony_ci			       struct emac_tx_queue *tx_q, struct emac_tpd *tpd)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	u32 *hw_tpd;
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	tx_q->tpd.last_produce_idx = tx_q->tpd.produce_idx;
10318c2ecf20Sopenharmony_ci	hw_tpd = EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.produce_idx);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	if (++tx_q->tpd.produce_idx == tx_q->tpd.count)
10348c2ecf20Sopenharmony_ci		tx_q->tpd.produce_idx = 0;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	*(hw_tpd++) = tpd->word[0];
10378c2ecf20Sopenharmony_ci	*(hw_tpd++) = tpd->word[1];
10388c2ecf20Sopenharmony_ci	*(hw_tpd++) = tpd->word[2];
10398c2ecf20Sopenharmony_ci	*hw_tpd = tpd->word[3];
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci/* Mark the last transmit descriptor as such (for the transmit packet) */
10438c2ecf20Sopenharmony_cistatic void emac_tx_tpd_mark_last(struct emac_adapter *adpt,
10448c2ecf20Sopenharmony_ci				  struct emac_tx_queue *tx_q)
10458c2ecf20Sopenharmony_ci{
10468c2ecf20Sopenharmony_ci	u32 *hw_tpd =
10478c2ecf20Sopenharmony_ci		EMAC_TPD(tx_q, adpt->tpd_size, tx_q->tpd.last_produce_idx);
10488c2ecf20Sopenharmony_ci	u32 tmp_tpd;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	tmp_tpd = *(hw_tpd + 1);
10518c2ecf20Sopenharmony_ci	tmp_tpd |= EMAC_TPD_LAST_FRAGMENT;
10528c2ecf20Sopenharmony_ci	*(hw_tpd + 1) = tmp_tpd;
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistatic void emac_rx_rfd_clean(struct emac_rx_queue *rx_q, struct emac_rrd *rrd)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct emac_buffer *rfbuf = rx_q->rfd.rfbuff;
10588c2ecf20Sopenharmony_ci	u32 consume_idx = RRD_SI(rrd);
10598c2ecf20Sopenharmony_ci	unsigned int i;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	for (i = 0; i < RRD_NOR(rrd); i++) {
10628c2ecf20Sopenharmony_ci		rfbuf[consume_idx].skb = NULL;
10638c2ecf20Sopenharmony_ci		if (++consume_idx == rx_q->rfd.count)
10648c2ecf20Sopenharmony_ci			consume_idx = 0;
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	rx_q->rfd.consume_idx = consume_idx;
10688c2ecf20Sopenharmony_ci	rx_q->rfd.process_idx = consume_idx;
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci/* Push the received skb to upper layers */
10728c2ecf20Sopenharmony_cistatic void emac_receive_skb(struct emac_rx_queue *rx_q,
10738c2ecf20Sopenharmony_ci			     struct sk_buff *skb,
10748c2ecf20Sopenharmony_ci			     u16 vlan_tag, bool vlan_flag)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	if (vlan_flag) {
10778c2ecf20Sopenharmony_ci		u16 vlan;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci		EMAC_TAG_TO_VLAN(vlan_tag, vlan);
10808c2ecf20Sopenharmony_ci		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
10818c2ecf20Sopenharmony_ci	}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	napi_gro_receive(&rx_q->napi, skb);
10848c2ecf20Sopenharmony_ci}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci/* Process receive event */
10878c2ecf20Sopenharmony_civoid emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
10888c2ecf20Sopenharmony_ci			 int *num_pkts, int max_pkts)
10898c2ecf20Sopenharmony_ci{
10908c2ecf20Sopenharmony_ci	u32 proc_idx, hw_consume_idx, num_consume_pkts;
10918c2ecf20Sopenharmony_ci	struct net_device *netdev  = adpt->netdev;
10928c2ecf20Sopenharmony_ci	struct emac_buffer *rfbuf;
10938c2ecf20Sopenharmony_ci	unsigned int count = 0;
10948c2ecf20Sopenharmony_ci	struct emac_rrd rrd;
10958c2ecf20Sopenharmony_ci	struct sk_buff *skb;
10968c2ecf20Sopenharmony_ci	u32 reg;
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	reg = readl_relaxed(adpt->base + rx_q->consume_reg);
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	hw_consume_idx = (reg & rx_q->consume_mask) >> rx_q->consume_shift;
11018c2ecf20Sopenharmony_ci	num_consume_pkts = (hw_consume_idx >= rx_q->rrd.consume_idx) ?
11028c2ecf20Sopenharmony_ci		(hw_consume_idx -  rx_q->rrd.consume_idx) :
11038c2ecf20Sopenharmony_ci		(hw_consume_idx + rx_q->rrd.count - rx_q->rrd.consume_idx);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	do {
11068c2ecf20Sopenharmony_ci		if (!num_consume_pkts)
11078c2ecf20Sopenharmony_ci			break;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci		if (!emac_rx_process_rrd(adpt, rx_q, &rrd))
11108c2ecf20Sopenharmony_ci			break;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci		if (likely(RRD_NOR(&rrd) == 1)) {
11138c2ecf20Sopenharmony_ci			/* good receive */
11148c2ecf20Sopenharmony_ci			rfbuf = GET_RFD_BUFFER(rx_q, RRD_SI(&rrd));
11158c2ecf20Sopenharmony_ci			dma_unmap_single(adpt->netdev->dev.parent,
11168c2ecf20Sopenharmony_ci					 rfbuf->dma_addr, rfbuf->length,
11178c2ecf20Sopenharmony_ci					 DMA_FROM_DEVICE);
11188c2ecf20Sopenharmony_ci			rfbuf->dma_addr = 0;
11198c2ecf20Sopenharmony_ci			skb = rfbuf->skb;
11208c2ecf20Sopenharmony_ci		} else {
11218c2ecf20Sopenharmony_ci			netdev_err(adpt->netdev,
11228c2ecf20Sopenharmony_ci				   "error: multi-RFD not support yet!\n");
11238c2ecf20Sopenharmony_ci			break;
11248c2ecf20Sopenharmony_ci		}
11258c2ecf20Sopenharmony_ci		emac_rx_rfd_clean(rx_q, &rrd);
11268c2ecf20Sopenharmony_ci		num_consume_pkts--;
11278c2ecf20Sopenharmony_ci		count++;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci		/* Due to a HW issue in L4 check sum detection (UDP/TCP frags
11308c2ecf20Sopenharmony_ci		 * with DF set are marked as error), drop packets based on the
11318c2ecf20Sopenharmony_ci		 * error mask rather than the summary bit (ignoring L4F errors)
11328c2ecf20Sopenharmony_ci		 */
11338c2ecf20Sopenharmony_ci		if (rrd.word[EMAC_RRD_STATS_DW_IDX] & EMAC_RRD_ERROR) {
11348c2ecf20Sopenharmony_ci			netif_dbg(adpt, rx_status, adpt->netdev,
11358c2ecf20Sopenharmony_ci				  "Drop error packet[RRD: 0x%x:0x%x:0x%x:0x%x]\n",
11368c2ecf20Sopenharmony_ci				  rrd.word[0], rrd.word[1],
11378c2ecf20Sopenharmony_ci				  rrd.word[2], rrd.word[3]);
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci			dev_kfree_skb(skb);
11408c2ecf20Sopenharmony_ci			continue;
11418c2ecf20Sopenharmony_ci		}
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci		skb_put(skb, RRD_PKT_SIZE(&rrd) - ETH_FCS_LEN);
11448c2ecf20Sopenharmony_ci		skb->dev = netdev;
11458c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, skb->dev);
11468c2ecf20Sopenharmony_ci		if (netdev->features & NETIF_F_RXCSUM)
11478c2ecf20Sopenharmony_ci			skb->ip_summed = RRD_L4F(&rrd) ?
11488c2ecf20Sopenharmony_ci					  CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
11498c2ecf20Sopenharmony_ci		else
11508c2ecf20Sopenharmony_ci			skb_checksum_none_assert(skb);
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci		emac_receive_skb(rx_q, skb, (u16)RRD_CVALN_TAG(&rrd),
11538c2ecf20Sopenharmony_ci				 (bool)RRD_CVTAG(&rrd));
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci		(*num_pkts)++;
11568c2ecf20Sopenharmony_ci	} while (*num_pkts < max_pkts);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	if (count) {
11598c2ecf20Sopenharmony_ci		proc_idx = (rx_q->rfd.process_idx << rx_q->process_shft) &
11608c2ecf20Sopenharmony_ci				rx_q->process_mask;
11618c2ecf20Sopenharmony_ci		emac_reg_update32(adpt->base + rx_q->process_reg,
11628c2ecf20Sopenharmony_ci				  rx_q->process_mask, proc_idx);
11638c2ecf20Sopenharmony_ci		emac_mac_rx_descs_refill(adpt, rx_q);
11648c2ecf20Sopenharmony_ci	}
11658c2ecf20Sopenharmony_ci}
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci/* get the number of free transmit descriptors */
11688c2ecf20Sopenharmony_cistatic unsigned int emac_tpd_num_free_descs(struct emac_tx_queue *tx_q)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	u32 produce_idx = tx_q->tpd.produce_idx;
11718c2ecf20Sopenharmony_ci	u32 consume_idx = tx_q->tpd.consume_idx;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	return (consume_idx > produce_idx) ?
11748c2ecf20Sopenharmony_ci		(consume_idx - produce_idx - 1) :
11758c2ecf20Sopenharmony_ci		(tx_q->tpd.count + consume_idx - produce_idx - 1);
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci/* Process transmit event */
11798c2ecf20Sopenharmony_civoid emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q)
11808c2ecf20Sopenharmony_ci{
11818c2ecf20Sopenharmony_ci	u32 reg = readl_relaxed(adpt->base + tx_q->consume_reg);
11828c2ecf20Sopenharmony_ci	u32 hw_consume_idx, pkts_compl = 0, bytes_compl = 0;
11838c2ecf20Sopenharmony_ci	struct emac_buffer *tpbuf;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	hw_consume_idx = (reg & tx_q->consume_mask) >> tx_q->consume_shift;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	while (tx_q->tpd.consume_idx != hw_consume_idx) {
11888c2ecf20Sopenharmony_ci		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.consume_idx);
11898c2ecf20Sopenharmony_ci		if (tpbuf->dma_addr) {
11908c2ecf20Sopenharmony_ci			dma_unmap_page(adpt->netdev->dev.parent,
11918c2ecf20Sopenharmony_ci				       tpbuf->dma_addr, tpbuf->length,
11928c2ecf20Sopenharmony_ci				       DMA_TO_DEVICE);
11938c2ecf20Sopenharmony_ci			tpbuf->dma_addr = 0;
11948c2ecf20Sopenharmony_ci		}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci		if (tpbuf->skb) {
11978c2ecf20Sopenharmony_ci			pkts_compl++;
11988c2ecf20Sopenharmony_ci			bytes_compl += tpbuf->skb->len;
11998c2ecf20Sopenharmony_ci			dev_consume_skb_irq(tpbuf->skb);
12008c2ecf20Sopenharmony_ci			tpbuf->skb = NULL;
12018c2ecf20Sopenharmony_ci		}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci		if (++tx_q->tpd.consume_idx == tx_q->tpd.count)
12048c2ecf20Sopenharmony_ci			tx_q->tpd.consume_idx = 0;
12058c2ecf20Sopenharmony_ci	}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	netdev_completed_queue(adpt->netdev, pkts_compl, bytes_compl);
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	if (netif_queue_stopped(adpt->netdev))
12108c2ecf20Sopenharmony_ci		if (emac_tpd_num_free_descs(tx_q) > (MAX_SKB_FRAGS + 1))
12118c2ecf20Sopenharmony_ci			netif_wake_queue(adpt->netdev);
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci/* Initialize all queue data structures */
12158c2ecf20Sopenharmony_civoid emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
12168c2ecf20Sopenharmony_ci				  struct emac_adapter *adpt)
12178c2ecf20Sopenharmony_ci{
12188c2ecf20Sopenharmony_ci	adpt->rx_q.netdev = adpt->netdev;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	adpt->rx_q.produce_reg  = EMAC_MAILBOX_0;
12218c2ecf20Sopenharmony_ci	adpt->rx_q.produce_mask = RFD0_PROD_IDX_BMSK;
12228c2ecf20Sopenharmony_ci	adpt->rx_q.produce_shift = RFD0_PROD_IDX_SHFT;
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	adpt->rx_q.process_reg  = EMAC_MAILBOX_0;
12258c2ecf20Sopenharmony_ci	adpt->rx_q.process_mask = RFD0_PROC_IDX_BMSK;
12268c2ecf20Sopenharmony_ci	adpt->rx_q.process_shft = RFD0_PROC_IDX_SHFT;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	adpt->rx_q.consume_reg  = EMAC_MAILBOX_3;
12298c2ecf20Sopenharmony_ci	adpt->rx_q.consume_mask = RFD0_CONS_IDX_BMSK;
12308c2ecf20Sopenharmony_ci	adpt->rx_q.consume_shift = RFD0_CONS_IDX_SHFT;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	adpt->rx_q.irq          = &adpt->irq;
12338c2ecf20Sopenharmony_ci	adpt->rx_q.intr         = adpt->irq.mask & ISR_RX_PKT;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	adpt->tx_q.produce_reg  = EMAC_MAILBOX_15;
12368c2ecf20Sopenharmony_ci	adpt->tx_q.produce_mask = NTPD_PROD_IDX_BMSK;
12378c2ecf20Sopenharmony_ci	adpt->tx_q.produce_shift = NTPD_PROD_IDX_SHFT;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	adpt->tx_q.consume_reg  = EMAC_MAILBOX_2;
12408c2ecf20Sopenharmony_ci	adpt->tx_q.consume_mask = NTPD_CONS_IDX_BMSK;
12418c2ecf20Sopenharmony_ci	adpt->tx_q.consume_shift = NTPD_CONS_IDX_SHFT;
12428c2ecf20Sopenharmony_ci}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci/* Fill up transmit descriptors with TSO and Checksum offload information */
12458c2ecf20Sopenharmony_cistatic int emac_tso_csum(struct emac_adapter *adpt,
12468c2ecf20Sopenharmony_ci			 struct emac_tx_queue *tx_q,
12478c2ecf20Sopenharmony_ci			 struct sk_buff *skb,
12488c2ecf20Sopenharmony_ci			 struct emac_tpd *tpd)
12498c2ecf20Sopenharmony_ci{
12508c2ecf20Sopenharmony_ci	unsigned int hdr_len;
12518c2ecf20Sopenharmony_ci	int ret;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (skb_is_gso(skb)) {
12548c2ecf20Sopenharmony_ci		if (skb_header_cloned(skb)) {
12558c2ecf20Sopenharmony_ci			ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
12568c2ecf20Sopenharmony_ci			if (unlikely(ret))
12578c2ecf20Sopenharmony_ci				return ret;
12588c2ecf20Sopenharmony_ci		}
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci		if (skb->protocol == htons(ETH_P_IP)) {
12618c2ecf20Sopenharmony_ci			u32 pkt_len = ((unsigned char *)ip_hdr(skb) - skb->data)
12628c2ecf20Sopenharmony_ci				       + ntohs(ip_hdr(skb)->tot_len);
12638c2ecf20Sopenharmony_ci			if (skb->len > pkt_len)
12648c2ecf20Sopenharmony_ci				pskb_trim(skb, pkt_len);
12658c2ecf20Sopenharmony_ci		}
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
12688c2ecf20Sopenharmony_ci		if (unlikely(skb->len == hdr_len)) {
12698c2ecf20Sopenharmony_ci			/* we only need to do csum */
12708c2ecf20Sopenharmony_ci			netif_warn(adpt, tx_err, adpt->netdev,
12718c2ecf20Sopenharmony_ci				   "tso not needed for packet with 0 data\n");
12728c2ecf20Sopenharmony_ci			goto do_csum;
12738c2ecf20Sopenharmony_ci		}
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
12768c2ecf20Sopenharmony_ci			ip_hdr(skb)->check = 0;
12778c2ecf20Sopenharmony_ci			tcp_hdr(skb)->check =
12788c2ecf20Sopenharmony_ci				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
12798c2ecf20Sopenharmony_ci						   ip_hdr(skb)->daddr,
12808c2ecf20Sopenharmony_ci						   0, IPPROTO_TCP, 0);
12818c2ecf20Sopenharmony_ci			TPD_IPV4_SET(tpd, 1);
12828c2ecf20Sopenharmony_ci		}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
12858c2ecf20Sopenharmony_ci			/* ipv6 tso need an extra tpd */
12868c2ecf20Sopenharmony_ci			struct emac_tpd extra_tpd;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci			memset(tpd, 0, sizeof(*tpd));
12898c2ecf20Sopenharmony_ci			memset(&extra_tpd, 0, sizeof(extra_tpd));
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci			tcp_v6_gso_csum_prep(skb);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci			TPD_PKT_LEN_SET(&extra_tpd, skb->len);
12948c2ecf20Sopenharmony_ci			TPD_LSO_SET(&extra_tpd, 1);
12958c2ecf20Sopenharmony_ci			TPD_LSOV_SET(&extra_tpd, 1);
12968c2ecf20Sopenharmony_ci			emac_tx_tpd_create(adpt, tx_q, &extra_tpd);
12978c2ecf20Sopenharmony_ci			TPD_LSOV_SET(tpd, 1);
12988c2ecf20Sopenharmony_ci		}
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci		TPD_LSO_SET(tpd, 1);
13018c2ecf20Sopenharmony_ci		TPD_TCPHDR_OFFSET_SET(tpd, skb_transport_offset(skb));
13028c2ecf20Sopenharmony_ci		TPD_MSS_SET(tpd, skb_shinfo(skb)->gso_size);
13038c2ecf20Sopenharmony_ci		return 0;
13048c2ecf20Sopenharmony_ci	}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_cido_csum:
13078c2ecf20Sopenharmony_ci	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
13088c2ecf20Sopenharmony_ci		unsigned int css, cso;
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci		cso = skb_transport_offset(skb);
13118c2ecf20Sopenharmony_ci		if (unlikely(cso & 0x1)) {
13128c2ecf20Sopenharmony_ci			netdev_err(adpt->netdev,
13138c2ecf20Sopenharmony_ci				   "error: payload offset should be even\n");
13148c2ecf20Sopenharmony_ci			return -EINVAL;
13158c2ecf20Sopenharmony_ci		}
13168c2ecf20Sopenharmony_ci		css = cso + skb->csum_offset;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci		TPD_PAYLOAD_OFFSET_SET(tpd, cso >> 1);
13198c2ecf20Sopenharmony_ci		TPD_CXSUM_OFFSET_SET(tpd, css >> 1);
13208c2ecf20Sopenharmony_ci		TPD_CSX_SET(tpd, 1);
13218c2ecf20Sopenharmony_ci	}
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	return 0;
13248c2ecf20Sopenharmony_ci}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci/* Fill up transmit descriptors */
13278c2ecf20Sopenharmony_cistatic void emac_tx_fill_tpd(struct emac_adapter *adpt,
13288c2ecf20Sopenharmony_ci			     struct emac_tx_queue *tx_q, struct sk_buff *skb,
13298c2ecf20Sopenharmony_ci			     struct emac_tpd *tpd)
13308c2ecf20Sopenharmony_ci{
13318c2ecf20Sopenharmony_ci	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
13328c2ecf20Sopenharmony_ci	unsigned int first = tx_q->tpd.produce_idx;
13338c2ecf20Sopenharmony_ci	unsigned int len = skb_headlen(skb);
13348c2ecf20Sopenharmony_ci	struct emac_buffer *tpbuf = NULL;
13358c2ecf20Sopenharmony_ci	unsigned int mapped_len = 0;
13368c2ecf20Sopenharmony_ci	unsigned int i;
13378c2ecf20Sopenharmony_ci	int count = 0;
13388c2ecf20Sopenharmony_ci	int ret;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	/* if Large Segment Offload is (in TCP Segmentation Offload struct) */
13418c2ecf20Sopenharmony_ci	if (TPD_LSO(tpd)) {
13428c2ecf20Sopenharmony_ci		mapped_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
13458c2ecf20Sopenharmony_ci		tpbuf->length = mapped_len;
13468c2ecf20Sopenharmony_ci		tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
13478c2ecf20Sopenharmony_ci					       virt_to_page(skb->data),
13488c2ecf20Sopenharmony_ci					       offset_in_page(skb->data),
13498c2ecf20Sopenharmony_ci					       tpbuf->length,
13508c2ecf20Sopenharmony_ci					       DMA_TO_DEVICE);
13518c2ecf20Sopenharmony_ci		ret = dma_mapping_error(adpt->netdev->dev.parent,
13528c2ecf20Sopenharmony_ci					tpbuf->dma_addr);
13538c2ecf20Sopenharmony_ci		if (ret)
13548c2ecf20Sopenharmony_ci			goto error;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
13578c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
13588c2ecf20Sopenharmony_ci		TPD_BUF_LEN_SET(tpd, tpbuf->length);
13598c2ecf20Sopenharmony_ci		emac_tx_tpd_create(adpt, tx_q, tpd);
13608c2ecf20Sopenharmony_ci		count++;
13618c2ecf20Sopenharmony_ci	}
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	if (mapped_len < len) {
13648c2ecf20Sopenharmony_ci		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
13658c2ecf20Sopenharmony_ci		tpbuf->length = len - mapped_len;
13668c2ecf20Sopenharmony_ci		tpbuf->dma_addr = dma_map_page(adpt->netdev->dev.parent,
13678c2ecf20Sopenharmony_ci					       virt_to_page(skb->data +
13688c2ecf20Sopenharmony_ci							    mapped_len),
13698c2ecf20Sopenharmony_ci					       offset_in_page(skb->data +
13708c2ecf20Sopenharmony_ci							      mapped_len),
13718c2ecf20Sopenharmony_ci					       tpbuf->length, DMA_TO_DEVICE);
13728c2ecf20Sopenharmony_ci		ret = dma_mapping_error(adpt->netdev->dev.parent,
13738c2ecf20Sopenharmony_ci					tpbuf->dma_addr);
13748c2ecf20Sopenharmony_ci		if (ret)
13758c2ecf20Sopenharmony_ci			goto error;
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
13788c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
13798c2ecf20Sopenharmony_ci		TPD_BUF_LEN_SET(tpd, tpbuf->length);
13808c2ecf20Sopenharmony_ci		emac_tx_tpd_create(adpt, tx_q, tpd);
13818c2ecf20Sopenharmony_ci		count++;
13828c2ecf20Sopenharmony_ci	}
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	for (i = 0; i < nr_frags; i++) {
13858c2ecf20Sopenharmony_ci		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci		tpbuf = GET_TPD_BUFFER(tx_q, tx_q->tpd.produce_idx);
13888c2ecf20Sopenharmony_ci		tpbuf->length = skb_frag_size(frag);
13898c2ecf20Sopenharmony_ci		tpbuf->dma_addr = skb_frag_dma_map(adpt->netdev->dev.parent,
13908c2ecf20Sopenharmony_ci						   frag, 0, tpbuf->length,
13918c2ecf20Sopenharmony_ci						   DMA_TO_DEVICE);
13928c2ecf20Sopenharmony_ci		ret = dma_mapping_error(adpt->netdev->dev.parent,
13938c2ecf20Sopenharmony_ci					tpbuf->dma_addr);
13948c2ecf20Sopenharmony_ci		if (ret)
13958c2ecf20Sopenharmony_ci			goto error;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_L_SET(tpd, lower_32_bits(tpbuf->dma_addr));
13988c2ecf20Sopenharmony_ci		TPD_BUFFER_ADDR_H_SET(tpd, upper_32_bits(tpbuf->dma_addr));
13998c2ecf20Sopenharmony_ci		TPD_BUF_LEN_SET(tpd, tpbuf->length);
14008c2ecf20Sopenharmony_ci		emac_tx_tpd_create(adpt, tx_q, tpd);
14018c2ecf20Sopenharmony_ci		count++;
14028c2ecf20Sopenharmony_ci	}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	/* The last tpd */
14058c2ecf20Sopenharmony_ci	wmb();
14068c2ecf20Sopenharmony_ci	emac_tx_tpd_mark_last(adpt, tx_q);
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	/* The last buffer info contain the skb address,
14098c2ecf20Sopenharmony_ci	 * so it will be freed after unmap
14108c2ecf20Sopenharmony_ci	 */
14118c2ecf20Sopenharmony_ci	tpbuf->skb = skb;
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	return;
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_cierror:
14168c2ecf20Sopenharmony_ci	/* One of the memory mappings failed, so undo everything */
14178c2ecf20Sopenharmony_ci	tx_q->tpd.produce_idx = first;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	while (count--) {
14208c2ecf20Sopenharmony_ci		tpbuf = GET_TPD_BUFFER(tx_q, first);
14218c2ecf20Sopenharmony_ci		dma_unmap_page(adpt->netdev->dev.parent, tpbuf->dma_addr,
14228c2ecf20Sopenharmony_ci			       tpbuf->length, DMA_TO_DEVICE);
14238c2ecf20Sopenharmony_ci		tpbuf->dma_addr = 0;
14248c2ecf20Sopenharmony_ci		tpbuf->length = 0;
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci		if (++first == tx_q->tpd.count)
14278c2ecf20Sopenharmony_ci			first = 0;
14288c2ecf20Sopenharmony_ci	}
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
14318c2ecf20Sopenharmony_ci}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci/* Transmit the packet using specified transmit queue */
14348c2ecf20Sopenharmony_cinetdev_tx_t emac_mac_tx_buf_send(struct emac_adapter *adpt,
14358c2ecf20Sopenharmony_ci				 struct emac_tx_queue *tx_q,
14368c2ecf20Sopenharmony_ci				 struct sk_buff *skb)
14378c2ecf20Sopenharmony_ci{
14388c2ecf20Sopenharmony_ci	struct emac_tpd tpd;
14398c2ecf20Sopenharmony_ci	u32 prod_idx;
14408c2ecf20Sopenharmony_ci	int len;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	memset(&tpd, 0, sizeof(tpd));
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	if (emac_tso_csum(adpt, tx_q, skb, &tpd) != 0) {
14458c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
14468c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
14478c2ecf20Sopenharmony_ci	}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	if (skb_vlan_tag_present(skb)) {
14508c2ecf20Sopenharmony_ci		u16 tag;
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci		EMAC_VLAN_TO_TAG(skb_vlan_tag_get(skb), tag);
14538c2ecf20Sopenharmony_ci		TPD_CVLAN_TAG_SET(&tpd, tag);
14548c2ecf20Sopenharmony_ci		TPD_INSTC_SET(&tpd, 1);
14558c2ecf20Sopenharmony_ci	}
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	if (skb_network_offset(skb) != ETH_HLEN)
14588c2ecf20Sopenharmony_ci		TPD_TYP_SET(&tpd, 1);
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	len = skb->len;
14618c2ecf20Sopenharmony_ci	emac_tx_fill_tpd(adpt, tx_q, skb, &tpd);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	netdev_sent_queue(adpt->netdev, len);
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	/* Make sure the are enough free descriptors to hold one
14668c2ecf20Sopenharmony_ci	 * maximum-sized SKB.  We need one desc for each fragment,
14678c2ecf20Sopenharmony_ci	 * one for the checksum (emac_tso_csum), one for TSO, and
14688c2ecf20Sopenharmony_ci	 * and one for the SKB header.
14698c2ecf20Sopenharmony_ci	 */
14708c2ecf20Sopenharmony_ci	if (emac_tpd_num_free_descs(tx_q) < (MAX_SKB_FRAGS + 3))
14718c2ecf20Sopenharmony_ci		netif_stop_queue(adpt->netdev);
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	/* update produce idx */
14748c2ecf20Sopenharmony_ci	prod_idx = (tx_q->tpd.produce_idx << tx_q->produce_shift) &
14758c2ecf20Sopenharmony_ci		    tx_q->produce_mask;
14768c2ecf20Sopenharmony_ci	emac_reg_update32(adpt->base + tx_q->produce_reg,
14778c2ecf20Sopenharmony_ci			  tx_q->produce_mask, prod_idx);
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
14808c2ecf20Sopenharmony_ci}
1481