18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014-2015 Hisilicon Limited.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __HNAE_H
78c2ecf20Sopenharmony_ci#define __HNAE_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/* Names used in this framework:
108c2ecf20Sopenharmony_ci *      ae handle (handle):
118c2ecf20Sopenharmony_ci *        a set of queues provided by AE
128c2ecf20Sopenharmony_ci *      ring buffer queue (rbq):
138c2ecf20Sopenharmony_ci *        the channel between upper layer and the AE, can do tx and rx
148c2ecf20Sopenharmony_ci *      ring:
158c2ecf20Sopenharmony_ci *        a tx or rx channel within a rbq
168c2ecf20Sopenharmony_ci *      ring description (desc):
178c2ecf20Sopenharmony_ci *        an element in the ring with packet information
188c2ecf20Sopenharmony_ci *      buffer:
198c2ecf20Sopenharmony_ci *        a memory region referred by desc with the full packet payload
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * "num" means a static number set as a parameter, "count" mean a dynamic
228c2ecf20Sopenharmony_ci *   number set while running
238c2ecf20Sopenharmony_ci * "cb" means control block
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/acpi.h>
278c2ecf20Sopenharmony_ci#include <linux/delay.h>
288c2ecf20Sopenharmony_ci#include <linux/device.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
318c2ecf20Sopenharmony_ci#include <linux/notifier.h>
328c2ecf20Sopenharmony_ci#include <linux/phy.h>
338c2ecf20Sopenharmony_ci#include <linux/types.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define HNAE_DRIVER_VERSION "2.0"
368c2ecf20Sopenharmony_ci#define HNAE_DRIVER_NAME "hns"
378c2ecf20Sopenharmony_ci#define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
388c2ecf20Sopenharmony_ci#define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
398c2ecf20Sopenharmony_ci#define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#ifdef DEBUG
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#ifndef assert
448c2ecf20Sopenharmony_ci#define assert(expr) \
458c2ecf20Sopenharmony_cido { \
468c2ecf20Sopenharmony_ci	if (!(expr)) { \
478c2ecf20Sopenharmony_ci		pr_err("Assertion failed! %s, %s, %s, line %d\n", \
488c2ecf20Sopenharmony_ci			   #expr, __FILE__, __func__, __LINE__); \
498c2ecf20Sopenharmony_ci	} \
508c2ecf20Sopenharmony_ci} while (0)
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#else
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#ifndef assert
568c2ecf20Sopenharmony_ci#define assert(expr)
578c2ecf20Sopenharmony_ci#endif
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#endif
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define AE_VERSION_1 ('6' << 16 | '6' << 8 | '0')
628c2ecf20Sopenharmony_ci#define AE_VERSION_2 ('1' << 24 | '6' << 16 | '1' << 8 | '0')
638c2ecf20Sopenharmony_ci#define AE_IS_VER1(ver) ((ver) == AE_VERSION_1)
648c2ecf20Sopenharmony_ci#define AE_NAME_SIZE 16
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define BD_SIZE_2048_MAX_MTU   6000
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* some said the RX and TX RCB format should not be the same in the future. But
698c2ecf20Sopenharmony_ci * it is the same now...
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci#define RCB_REG_BASEADDR_L         0x00 /* P660 support only 32bit accessing */
728c2ecf20Sopenharmony_ci#define RCB_REG_BASEADDR_H         0x04
738c2ecf20Sopenharmony_ci#define RCB_REG_BD_NUM             0x08
748c2ecf20Sopenharmony_ci#define RCB_REG_BD_LEN             0x0C
758c2ecf20Sopenharmony_ci#define RCB_REG_PKTLINE            0x10
768c2ecf20Sopenharmony_ci#define RCB_REG_TAIL               0x18
778c2ecf20Sopenharmony_ci#define RCB_REG_HEAD               0x1C
788c2ecf20Sopenharmony_ci#define RCB_REG_FBDNUM             0x20
798c2ecf20Sopenharmony_ci#define RCB_REG_OFFSET             0x24 /* pkt num to be handled */
808c2ecf20Sopenharmony_ci#define RCB_REG_PKTNUM_RECORD      0x2C /* total pkt received */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define HNS_RX_HEAD_SIZE 256
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define HNAE_AE_REGISTER 0x1
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define RCB_RING_NAME_LEN (IFNAMSIZ + 4)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define HNAE_LOWEST_LATENCY_COAL_PARAM	30
898c2ecf20Sopenharmony_ci#define HNAE_LOW_LATENCY_COAL_PARAM	80
908c2ecf20Sopenharmony_ci#define HNAE_BULK_LATENCY_COAL_PARAM	150
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cienum hnae_led_state {
938c2ecf20Sopenharmony_ci	HNAE_LED_INACTIVE,
948c2ecf20Sopenharmony_ci	HNAE_LED_ACTIVE,
958c2ecf20Sopenharmony_ci	HNAE_LED_ON,
968c2ecf20Sopenharmony_ci	HNAE_LED_OFF
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_VLAN_PRESENT 0x1
1008c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_L3ID_IPV4 0x0
1018c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_L3ID_IPV6 0x1
1028c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_L4ID_UDP 0x0
1038c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_L4ID_TCP 0x1
1048c2ecf20Sopenharmony_ci#define HNS_RX_FLAG_L4ID_SCTP 0x3
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define HNS_TXD_ASID_S 0
1078c2ecf20Sopenharmony_ci#define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
1088c2ecf20Sopenharmony_ci#define HNS_TXD_BUFNUM_S 8
1098c2ecf20Sopenharmony_ci#define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
1108c2ecf20Sopenharmony_ci#define HNS_TXD_PORTID_S 10
1118c2ecf20Sopenharmony_ci#define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define HNS_TXD_RA_B 8
1148c2ecf20Sopenharmony_ci#define HNS_TXD_RI_B 9
1158c2ecf20Sopenharmony_ci#define HNS_TXD_L4CS_B 10
1168c2ecf20Sopenharmony_ci#define HNS_TXD_L3CS_B 11
1178c2ecf20Sopenharmony_ci#define HNS_TXD_FE_B 12
1188c2ecf20Sopenharmony_ci#define HNS_TXD_VLD_B 13
1198c2ecf20Sopenharmony_ci#define HNS_TXD_IPOFFSET_S 14
1208c2ecf20Sopenharmony_ci#define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define HNS_RXD_IPOFFSET_S 0
1238c2ecf20Sopenharmony_ci#define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
1248c2ecf20Sopenharmony_ci#define HNS_RXD_BUFNUM_S 8
1258c2ecf20Sopenharmony_ci#define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
1268c2ecf20Sopenharmony_ci#define HNS_RXD_PORTID_S 10
1278c2ecf20Sopenharmony_ci#define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
1288c2ecf20Sopenharmony_ci#define HNS_RXD_DMAC_S 13
1298c2ecf20Sopenharmony_ci#define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
1308c2ecf20Sopenharmony_ci#define HNS_RXD_VLAN_S 15
1318c2ecf20Sopenharmony_ci#define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
1328c2ecf20Sopenharmony_ci#define HNS_RXD_L3ID_S 17
1338c2ecf20Sopenharmony_ci#define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
1348c2ecf20Sopenharmony_ci#define HNS_RXD_L4ID_S 21
1358c2ecf20Sopenharmony_ci#define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
1368c2ecf20Sopenharmony_ci#define HNS_RXD_FE_B 25
1378c2ecf20Sopenharmony_ci#define HNS_RXD_FRAG_B 26
1388c2ecf20Sopenharmony_ci#define HNS_RXD_VLD_B 27
1398c2ecf20Sopenharmony_ci#define HNS_RXD_L2E_B 28
1408c2ecf20Sopenharmony_ci#define HNS_RXD_L3E_B 29
1418c2ecf20Sopenharmony_ci#define HNS_RXD_L4E_B 30
1428c2ecf20Sopenharmony_ci#define HNS_RXD_DROP_B 31
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define HNS_RXD_VLANID_S 8
1458c2ecf20Sopenharmony_ci#define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
1468c2ecf20Sopenharmony_ci#define HNS_RXD_CFI_B 20
1478c2ecf20Sopenharmony_ci#define HNS_RXD_PRI_S 21
1488c2ecf20Sopenharmony_ci#define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
1498c2ecf20Sopenharmony_ci#define HNS_RXD_ASID_S 24
1508c2ecf20Sopenharmony_ci#define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define HNSV2_TXD_BUFNUM_S 0
1538c2ecf20Sopenharmony_ci#define HNSV2_TXD_BUFNUM_M (0x7 << HNSV2_TXD_BUFNUM_S)
1548c2ecf20Sopenharmony_ci#define HNSV2_TXD_PORTID_S	4
1558c2ecf20Sopenharmony_ci#define HNSV2_TXD_PORTID_M	(0X7 << HNSV2_TXD_PORTID_S)
1568c2ecf20Sopenharmony_ci#define HNSV2_TXD_RI_B   1
1578c2ecf20Sopenharmony_ci#define HNSV2_TXD_L4CS_B   2
1588c2ecf20Sopenharmony_ci#define HNSV2_TXD_L3CS_B   3
1598c2ecf20Sopenharmony_ci#define HNSV2_TXD_FE_B   4
1608c2ecf20Sopenharmony_ci#define HNSV2_TXD_VLD_B  5
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci#define HNSV2_TXD_TSE_B   0
1638c2ecf20Sopenharmony_ci#define HNSV2_TXD_VLAN_EN_B   1
1648c2ecf20Sopenharmony_ci#define HNSV2_TXD_SNAP_B   2
1658c2ecf20Sopenharmony_ci#define HNSV2_TXD_IPV6_B   3
1668c2ecf20Sopenharmony_ci#define HNSV2_TXD_SCTP_B   4
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* hardware spec ring buffer format */
1698c2ecf20Sopenharmony_cistruct __packed hnae_desc {
1708c2ecf20Sopenharmony_ci	__le64 addr;
1718c2ecf20Sopenharmony_ci	union {
1728c2ecf20Sopenharmony_ci		struct {
1738c2ecf20Sopenharmony_ci			union {
1748c2ecf20Sopenharmony_ci				__le16 asid_bufnum_pid;
1758c2ecf20Sopenharmony_ci				__le16 asid;
1768c2ecf20Sopenharmony_ci			};
1778c2ecf20Sopenharmony_ci			__le16 send_size;
1788c2ecf20Sopenharmony_ci			union {
1798c2ecf20Sopenharmony_ci				__le32 flag_ipoffset;
1808c2ecf20Sopenharmony_ci				struct {
1818c2ecf20Sopenharmony_ci					__u8 bn_pid;
1828c2ecf20Sopenharmony_ci					__u8 ra_ri_cs_fe_vld;
1838c2ecf20Sopenharmony_ci					__u8 ip_offset;
1848c2ecf20Sopenharmony_ci					__u8 tse_vlan_snap_v6_sctp_nth;
1858c2ecf20Sopenharmony_ci				};
1868c2ecf20Sopenharmony_ci			};
1878c2ecf20Sopenharmony_ci			__le16 mss;
1888c2ecf20Sopenharmony_ci			__u8 l4_len;
1898c2ecf20Sopenharmony_ci			__u8 reserved1;
1908c2ecf20Sopenharmony_ci			__le16 paylen;
1918c2ecf20Sopenharmony_ci			__u8 vmid;
1928c2ecf20Sopenharmony_ci			__u8 qid;
1938c2ecf20Sopenharmony_ci			__le32 reserved2[2];
1948c2ecf20Sopenharmony_ci		} tx;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		struct {
1978c2ecf20Sopenharmony_ci			__le32 ipoff_bnum_pid_flag;
1988c2ecf20Sopenharmony_ci			__le16 pkt_len;
1998c2ecf20Sopenharmony_ci			__le16 size;
2008c2ecf20Sopenharmony_ci			union {
2018c2ecf20Sopenharmony_ci				__le32 vlan_pri_asid;
2028c2ecf20Sopenharmony_ci				struct {
2038c2ecf20Sopenharmony_ci					__le16 asid;
2048c2ecf20Sopenharmony_ci					__le16 vlan_cfi_pri;
2058c2ecf20Sopenharmony_ci				};
2068c2ecf20Sopenharmony_ci			};
2078c2ecf20Sopenharmony_ci			__le32 rss_hash;
2088c2ecf20Sopenharmony_ci			__le32 reserved_1[2];
2098c2ecf20Sopenharmony_ci		} rx;
2108c2ecf20Sopenharmony_ci	};
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistruct hnae_desc_cb {
2148c2ecf20Sopenharmony_ci	dma_addr_t dma; /* dma address of this desc */
2158c2ecf20Sopenharmony_ci	void *buf;      /* cpu addr for a desc */
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* priv data for the desc, e.g. skb when use with ip stack*/
2188c2ecf20Sopenharmony_ci	void *priv;
2198c2ecf20Sopenharmony_ci	u32 page_offset;
2208c2ecf20Sopenharmony_ci	u32 length;     /* length of the buffer */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	u16 reuse_flag;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci       /* desc type, used by the ring user to mark the type of the priv data */
2258c2ecf20Sopenharmony_ci	u16 type;
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci#define setflags(flags, bits) ((flags) |= (bits))
2298c2ecf20Sopenharmony_ci#define unsetflags(flags, bits) ((flags) &= ~(bits))
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/* hnae_ring->flags fields */
2328c2ecf20Sopenharmony_ci#define RINGF_DIR 0x1	    /* TX or RX ring, set if TX */
2338c2ecf20Sopenharmony_ci#define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
2348c2ecf20Sopenharmony_ci#define is_rx_ring(ring) (!is_tx_ring(ring))
2358c2ecf20Sopenharmony_ci#define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
2368c2ecf20Sopenharmony_ci	DMA_TO_DEVICE : DMA_FROM_DEVICE)
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistruct ring_stats {
2398c2ecf20Sopenharmony_ci	u64 io_err_cnt;
2408c2ecf20Sopenharmony_ci	u64 sw_err_cnt;
2418c2ecf20Sopenharmony_ci	u64 seg_pkt_cnt;
2428c2ecf20Sopenharmony_ci	union {
2438c2ecf20Sopenharmony_ci		struct {
2448c2ecf20Sopenharmony_ci			u64 tx_pkts;
2458c2ecf20Sopenharmony_ci			u64 tx_bytes;
2468c2ecf20Sopenharmony_ci			u64 tx_err_cnt;
2478c2ecf20Sopenharmony_ci			u64 restart_queue;
2488c2ecf20Sopenharmony_ci			u64 tx_busy;
2498c2ecf20Sopenharmony_ci		};
2508c2ecf20Sopenharmony_ci		struct {
2518c2ecf20Sopenharmony_ci			u64 rx_pkts;
2528c2ecf20Sopenharmony_ci			u64 rx_bytes;
2538c2ecf20Sopenharmony_ci			u64 rx_err_cnt;
2548c2ecf20Sopenharmony_ci			u64 reuse_pg_cnt;
2558c2ecf20Sopenharmony_ci			u64 err_pkt_len;
2568c2ecf20Sopenharmony_ci			u64 non_vld_descs;
2578c2ecf20Sopenharmony_ci			u64 err_bd_num;
2588c2ecf20Sopenharmony_ci			u64 l2_err;
2598c2ecf20Sopenharmony_ci			u64 l3l4_csum_err;
2608c2ecf20Sopenharmony_ci		};
2618c2ecf20Sopenharmony_ci	};
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistruct hnae_queue;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistruct hnae_ring {
2678c2ecf20Sopenharmony_ci	u8 __iomem *io_base; /* base io address for the ring */
2688c2ecf20Sopenharmony_ci	struct hnae_desc *desc; /* dma map address space */
2698c2ecf20Sopenharmony_ci	struct hnae_desc_cb *desc_cb;
2708c2ecf20Sopenharmony_ci	struct hnae_queue *q;
2718c2ecf20Sopenharmony_ci	int irq;
2728c2ecf20Sopenharmony_ci	char ring_name[RCB_RING_NAME_LEN];
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* statistic */
2758c2ecf20Sopenharmony_ci	struct ring_stats stats;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	dma_addr_t desc_dma_addr;
2788c2ecf20Sopenharmony_ci	u32 buf_size;       /* size for hnae_desc->addr, preset by AE */
2798c2ecf20Sopenharmony_ci	u16 desc_num;       /* total number of desc */
2808c2ecf20Sopenharmony_ci	u16 max_desc_num_per_pkt;
2818c2ecf20Sopenharmony_ci	u16 max_raw_data_sz_per_desc;
2828c2ecf20Sopenharmony_ci	u16 max_pkt_size;
2838c2ecf20Sopenharmony_ci	int next_to_use;    /* idx of next spare desc */
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* idx of lastest sent desc, the ring is empty when equal to
2868c2ecf20Sopenharmony_ci	 * next_to_use
2878c2ecf20Sopenharmony_ci	 */
2888c2ecf20Sopenharmony_ci	int next_to_clean;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	int flags;          /* ring attribute */
2918c2ecf20Sopenharmony_ci	int irq_init_flag;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* total rx bytes after last rx rate calucated */
2948c2ecf20Sopenharmony_ci	u64 coal_last_rx_bytes;
2958c2ecf20Sopenharmony_ci	unsigned long coal_last_jiffies;
2968c2ecf20Sopenharmony_ci	u32 coal_param;
2978c2ecf20Sopenharmony_ci	u32 coal_rx_rate;	/* rx rate in MB */
2988c2ecf20Sopenharmony_ci};
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci#define ring_ptr_move_fw(ring, p) \
3018c2ecf20Sopenharmony_ci	((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
3028c2ecf20Sopenharmony_ci#define ring_ptr_move_bw(ring, p) \
3038c2ecf20Sopenharmony_ci	((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cienum hns_desc_type {
3068c2ecf20Sopenharmony_ci	DESC_TYPE_SKB,
3078c2ecf20Sopenharmony_ci	DESC_TYPE_PAGE,
3088c2ecf20Sopenharmony_ci};
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci#define assert_is_ring_idx(ring, idx) \
3118c2ecf20Sopenharmony_ci	assert((idx) >= 0 && (idx) < (ring)->desc_num)
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/* the distance between [begin, end) in a ring buffer
3148c2ecf20Sopenharmony_ci * note: there is a unuse slot between the begin and the end
3158c2ecf20Sopenharmony_ci */
3168c2ecf20Sopenharmony_cistatic inline int ring_dist(struct hnae_ring *ring, int begin, int end)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	assert_is_ring_idx(ring, begin);
3198c2ecf20Sopenharmony_ci	assert_is_ring_idx(ring, end);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return (end - begin + ring->desc_num) % ring->desc_num;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic inline int ring_space(struct hnae_ring *ring)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	return ring->desc_num -
3278c2ecf20Sopenharmony_ci		ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic inline int is_ring_empty(struct hnae_ring *ring)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	assert_is_ring_idx(ring, ring->next_to_use);
3338c2ecf20Sopenharmony_ci	assert_is_ring_idx(ring, ring->next_to_clean);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	return ring->next_to_use == ring->next_to_clean;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci#define hnae_buf_size(_ring) ((_ring)->buf_size)
3398c2ecf20Sopenharmony_ci#define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
3408c2ecf20Sopenharmony_ci#define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistruct hnae_handle;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci/* allocate and dma map space for hnae desc */
3458c2ecf20Sopenharmony_cistruct hnae_buf_ops {
3468c2ecf20Sopenharmony_ci	int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
3478c2ecf20Sopenharmony_ci	void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
3488c2ecf20Sopenharmony_ci	int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
3498c2ecf20Sopenharmony_ci	void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
3508c2ecf20Sopenharmony_ci};
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistruct hnae_queue {
3538c2ecf20Sopenharmony_ci	u8 __iomem *io_base;
3548c2ecf20Sopenharmony_ci	phys_addr_t phy_base;
3558c2ecf20Sopenharmony_ci	struct hnae_ae_dev *dev;	/* the device who use this queue */
3568c2ecf20Sopenharmony_ci	struct hnae_ring rx_ring ____cacheline_internodealigned_in_smp;
3578c2ecf20Sopenharmony_ci	struct hnae_ring tx_ring ____cacheline_internodealigned_in_smp;
3588c2ecf20Sopenharmony_ci	struct hnae_handle *handle;
3598c2ecf20Sopenharmony_ci};
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci/*hnae loop mode*/
3628c2ecf20Sopenharmony_cienum hnae_loop {
3638c2ecf20Sopenharmony_ci	MAC_INTERNALLOOP_MAC = 0,
3648c2ecf20Sopenharmony_ci	MAC_INTERNALLOOP_SERDES,
3658c2ecf20Sopenharmony_ci	MAC_INTERNALLOOP_PHY,
3668c2ecf20Sopenharmony_ci	MAC_LOOP_PHY_NONE,
3678c2ecf20Sopenharmony_ci	MAC_LOOP_NONE,
3688c2ecf20Sopenharmony_ci};
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/*hnae port type*/
3718c2ecf20Sopenharmony_cienum hnae_port_type {
3728c2ecf20Sopenharmony_ci	HNAE_PORT_SERVICE = 0,
3738c2ecf20Sopenharmony_ci	HNAE_PORT_DEBUG
3748c2ecf20Sopenharmony_ci};
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/* mac media type */
3778c2ecf20Sopenharmony_cienum hnae_media_type {
3788c2ecf20Sopenharmony_ci	HNAE_MEDIA_TYPE_UNKNOWN = 0,
3798c2ecf20Sopenharmony_ci	HNAE_MEDIA_TYPE_FIBER,
3808c2ecf20Sopenharmony_ci	HNAE_MEDIA_TYPE_COPPER,
3818c2ecf20Sopenharmony_ci	HNAE_MEDIA_TYPE_BACKPLANE,
3828c2ecf20Sopenharmony_ci};
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/* This struct defines the operation on the handle.
3858c2ecf20Sopenharmony_ci *
3868c2ecf20Sopenharmony_ci * get_handle(): (mandatory)
3878c2ecf20Sopenharmony_ci *   Get a handle from AE according to its name and options.
3888c2ecf20Sopenharmony_ci *   the AE driver should manage the space used by handle and its queues while
3898c2ecf20Sopenharmony_ci *   the HNAE framework will allocate desc and desc_cb for all rings in the
3908c2ecf20Sopenharmony_ci *   queues.
3918c2ecf20Sopenharmony_ci * put_handle():
3928c2ecf20Sopenharmony_ci *   Release the handle.
3938c2ecf20Sopenharmony_ci * start():
3948c2ecf20Sopenharmony_ci *   Enable the hardware, include all queues
3958c2ecf20Sopenharmony_ci * stop():
3968c2ecf20Sopenharmony_ci *   Disable the hardware
3978c2ecf20Sopenharmony_ci * set_opts(): (mandatory)
3988c2ecf20Sopenharmony_ci *   Set options to the AE
3998c2ecf20Sopenharmony_ci * get_opts(): (mandatory)
4008c2ecf20Sopenharmony_ci *   Get options from the AE
4018c2ecf20Sopenharmony_ci * get_status():
4028c2ecf20Sopenharmony_ci *   Get the carrier state of the back channel of the handle, 1 for ok, 0 for
4038c2ecf20Sopenharmony_ci *   non-ok
4048c2ecf20Sopenharmony_ci * toggle_ring_irq(): (mandatory)
4058c2ecf20Sopenharmony_ci *   Set the ring irq to be enabled(0) or disable(1)
4068c2ecf20Sopenharmony_ci * toggle_queue_status(): (mandatory)
4078c2ecf20Sopenharmony_ci *   Set the queue to be enabled(1) or disable(0), this will not change the
4088c2ecf20Sopenharmony_ci *   ring irq state
4098c2ecf20Sopenharmony_ci * adjust_link()
4108c2ecf20Sopenharmony_ci *   adjust link status
4118c2ecf20Sopenharmony_ci * set_loopback()
4128c2ecf20Sopenharmony_ci *   set loopback
4138c2ecf20Sopenharmony_ci * get_ring_bdnum_limit()
4148c2ecf20Sopenharmony_ci *   get ring bd number limit
4158c2ecf20Sopenharmony_ci * get_pauseparam()
4168c2ecf20Sopenharmony_ci *   get tx and rx of pause frame use
4178c2ecf20Sopenharmony_ci * set_autoneg()
4188c2ecf20Sopenharmony_ci *   set auto autonegotiation of pause frame use
4198c2ecf20Sopenharmony_ci * get_autoneg()
4208c2ecf20Sopenharmony_ci *   get auto autonegotiation of pause frame use
4218c2ecf20Sopenharmony_ci * set_pauseparam()
4228c2ecf20Sopenharmony_ci *   set tx and rx of pause frame use
4238c2ecf20Sopenharmony_ci * get_coalesce_usecs()
4248c2ecf20Sopenharmony_ci *   get usecs to delay a TX interrupt after a packet is sent
4258c2ecf20Sopenharmony_ci * get_rx_max_coalesced_frames()
4268c2ecf20Sopenharmony_ci *   get Maximum number of packets to be sent before a TX interrupt.
4278c2ecf20Sopenharmony_ci * set_coalesce_usecs()
4288c2ecf20Sopenharmony_ci *   set usecs to delay a TX interrupt after a packet is sent
4298c2ecf20Sopenharmony_ci * set_coalesce_frames()
4308c2ecf20Sopenharmony_ci *   set Maximum number of packets to be sent before a TX interrupt.
4318c2ecf20Sopenharmony_ci * get_ringnum()
4328c2ecf20Sopenharmony_ci *   get RX/TX ring number
4338c2ecf20Sopenharmony_ci * get_max_ringnum()
4348c2ecf20Sopenharmony_ci *   get RX/TX ring maximum number
4358c2ecf20Sopenharmony_ci * get_mac_addr()
4368c2ecf20Sopenharmony_ci *   get mac address
4378c2ecf20Sopenharmony_ci * set_mac_addr()
4388c2ecf20Sopenharmony_ci *   set mac address
4398c2ecf20Sopenharmony_ci * clr_mc_addr()
4408c2ecf20Sopenharmony_ci *   clear mcast tcam table
4418c2ecf20Sopenharmony_ci * set_mc_addr()
4428c2ecf20Sopenharmony_ci *   set multicast mode
4438c2ecf20Sopenharmony_ci * add_uc_addr()
4448c2ecf20Sopenharmony_ci *   add ucast address
4458c2ecf20Sopenharmony_ci * rm_uc_addr()
4468c2ecf20Sopenharmony_ci *   remove ucast address
4478c2ecf20Sopenharmony_ci * set_mtu()
4488c2ecf20Sopenharmony_ci *   set mtu
4498c2ecf20Sopenharmony_ci * update_stats()
4508c2ecf20Sopenharmony_ci *   update Old network device statistics
4518c2ecf20Sopenharmony_ci * get_ethtool_stats()
4528c2ecf20Sopenharmony_ci *   get ethtool network device statistics
4538c2ecf20Sopenharmony_ci * get_strings()
4548c2ecf20Sopenharmony_ci *   get a set of strings that describe the requested objects
4558c2ecf20Sopenharmony_ci * get_sset_count()
4568c2ecf20Sopenharmony_ci *   get number of strings that @get_strings will write
4578c2ecf20Sopenharmony_ci * update_led_status()
4588c2ecf20Sopenharmony_ci *   update the led status
4598c2ecf20Sopenharmony_ci * set_led_id()
4608c2ecf20Sopenharmony_ci *   set led id
4618c2ecf20Sopenharmony_ci * get_regs()
4628c2ecf20Sopenharmony_ci *   get regs dump
4638c2ecf20Sopenharmony_ci * get_regs_len()
4648c2ecf20Sopenharmony_ci *   get the len of the regs dump
4658c2ecf20Sopenharmony_ci */
4668c2ecf20Sopenharmony_cistruct hnae_ae_ops {
4678c2ecf20Sopenharmony_ci	struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
4688c2ecf20Sopenharmony_ci					  u32 port_id);
4698c2ecf20Sopenharmony_ci	void (*put_handle)(struct hnae_handle *handle);
4708c2ecf20Sopenharmony_ci	void (*init_queue)(struct hnae_queue *q);
4718c2ecf20Sopenharmony_ci	void (*fini_queue)(struct hnae_queue *q);
4728c2ecf20Sopenharmony_ci	int (*start)(struct hnae_handle *handle);
4738c2ecf20Sopenharmony_ci	void (*stop)(struct hnae_handle *handle);
4748c2ecf20Sopenharmony_ci	void (*reset)(struct hnae_handle *handle);
4758c2ecf20Sopenharmony_ci	int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
4768c2ecf20Sopenharmony_ci	int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
4778c2ecf20Sopenharmony_ci	int (*get_status)(struct hnae_handle *handle);
4788c2ecf20Sopenharmony_ci	int (*get_info)(struct hnae_handle *handle,
4798c2ecf20Sopenharmony_ci			u8 *auto_neg, u16 *speed, u8 *duplex);
4808c2ecf20Sopenharmony_ci	void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
4818c2ecf20Sopenharmony_ci	void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
4828c2ecf20Sopenharmony_ci	bool (*need_adjust_link)(struct hnae_handle *handle,
4838c2ecf20Sopenharmony_ci				 int speed, int duplex);
4848c2ecf20Sopenharmony_ci	int (*set_loopback)(struct hnae_handle *handle,
4858c2ecf20Sopenharmony_ci			    enum hnae_loop loop_mode, int en);
4868c2ecf20Sopenharmony_ci	void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
4878c2ecf20Sopenharmony_ci				     u32 *uplimit);
4888c2ecf20Sopenharmony_ci	void (*get_pauseparam)(struct hnae_handle *handle,
4898c2ecf20Sopenharmony_ci			       u32 *auto_neg, u32 *rx_en, u32 *tx_en);
4908c2ecf20Sopenharmony_ci	int (*set_autoneg)(struct hnae_handle *handle, u8 enable);
4918c2ecf20Sopenharmony_ci	int (*get_autoneg)(struct hnae_handle *handle);
4928c2ecf20Sopenharmony_ci	int (*set_pauseparam)(struct hnae_handle *handle,
4938c2ecf20Sopenharmony_ci			      u32 auto_neg, u32 rx_en, u32 tx_en);
4948c2ecf20Sopenharmony_ci	void (*get_coalesce_usecs)(struct hnae_handle *handle,
4958c2ecf20Sopenharmony_ci				   u32 *tx_usecs, u32 *rx_usecs);
4968c2ecf20Sopenharmony_ci	void (*get_max_coalesced_frames)(struct hnae_handle *handle,
4978c2ecf20Sopenharmony_ci					 u32 *tx_frames, u32 *rx_frames);
4988c2ecf20Sopenharmony_ci	int (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
4998c2ecf20Sopenharmony_ci	int (*set_coalesce_frames)(struct hnae_handle *handle,
5008c2ecf20Sopenharmony_ci				   u32 tx_frames, u32 rx_frames);
5018c2ecf20Sopenharmony_ci	void (*get_coalesce_range)(struct hnae_handle *handle,
5028c2ecf20Sopenharmony_ci				   u32 *tx_frames_low, u32 *rx_frames_low,
5038c2ecf20Sopenharmony_ci				   u32 *tx_frames_high, u32 *rx_frames_high,
5048c2ecf20Sopenharmony_ci				   u32 *tx_usecs_low, u32 *rx_usecs_low,
5058c2ecf20Sopenharmony_ci				   u32 *tx_usecs_high, u32 *rx_usecs_high);
5068c2ecf20Sopenharmony_ci	void (*set_promisc_mode)(struct hnae_handle *handle, u32 en);
5078c2ecf20Sopenharmony_ci	int (*get_mac_addr)(struct hnae_handle *handle, void **p);
5088c2ecf20Sopenharmony_ci	int (*set_mac_addr)(struct hnae_handle *handle, void *p);
5098c2ecf20Sopenharmony_ci	int (*add_uc_addr)(struct hnae_handle *handle,
5108c2ecf20Sopenharmony_ci			   const unsigned char *addr);
5118c2ecf20Sopenharmony_ci	int (*rm_uc_addr)(struct hnae_handle *handle,
5128c2ecf20Sopenharmony_ci			  const unsigned char *addr);
5138c2ecf20Sopenharmony_ci	int (*clr_mc_addr)(struct hnae_handle *handle);
5148c2ecf20Sopenharmony_ci	int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
5158c2ecf20Sopenharmony_ci	int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
5168c2ecf20Sopenharmony_ci	void (*set_tso_stats)(struct hnae_handle *handle, int enable);
5178c2ecf20Sopenharmony_ci	void (*update_stats)(struct hnae_handle *handle,
5188c2ecf20Sopenharmony_ci			     struct net_device_stats *net_stats);
5198c2ecf20Sopenharmony_ci	void (*get_stats)(struct hnae_handle *handle, u64 *data);
5208c2ecf20Sopenharmony_ci	void (*get_strings)(struct hnae_handle *handle,
5218c2ecf20Sopenharmony_ci			    u32 stringset, u8 *data);
5228c2ecf20Sopenharmony_ci	int (*get_sset_count)(struct hnae_handle *handle, int stringset);
5238c2ecf20Sopenharmony_ci	void (*update_led_status)(struct hnae_handle *handle);
5248c2ecf20Sopenharmony_ci	int (*set_led_id)(struct hnae_handle *handle,
5258c2ecf20Sopenharmony_ci			  enum hnae_led_state status);
5268c2ecf20Sopenharmony_ci	void (*get_regs)(struct hnae_handle *handle, void *data);
5278c2ecf20Sopenharmony_ci	int (*get_regs_len)(struct hnae_handle *handle);
5288c2ecf20Sopenharmony_ci	u32	(*get_rss_key_size)(struct hnae_handle *handle);
5298c2ecf20Sopenharmony_ci	u32	(*get_rss_indir_size)(struct hnae_handle *handle);
5308c2ecf20Sopenharmony_ci	int	(*get_rss)(struct hnae_handle *handle, u32 *indir, u8 *key,
5318c2ecf20Sopenharmony_ci			   u8 *hfunc);
5328c2ecf20Sopenharmony_ci	int	(*set_rss)(struct hnae_handle *handle, const u32 *indir,
5338c2ecf20Sopenharmony_ci			   const u8 *key, const u8 hfunc);
5348c2ecf20Sopenharmony_ci};
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistruct hnae_ae_dev {
5378c2ecf20Sopenharmony_ci	struct device cls_dev; /* the class dev */
5388c2ecf20Sopenharmony_ci	struct device *dev; /* the presented dev */
5398c2ecf20Sopenharmony_ci	struct hnae_ae_ops *ops;
5408c2ecf20Sopenharmony_ci	struct list_head node;
5418c2ecf20Sopenharmony_ci	struct module *owner; /* the module who provides this dev */
5428c2ecf20Sopenharmony_ci	int id;
5438c2ecf20Sopenharmony_ci	char name[AE_NAME_SIZE];
5448c2ecf20Sopenharmony_ci	struct list_head handle_list;
5458c2ecf20Sopenharmony_ci	spinlock_t lock; /* lock to protect the handle_list */
5468c2ecf20Sopenharmony_ci};
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistruct hnae_handle {
5498c2ecf20Sopenharmony_ci	struct device *owner_dev; /* the device which make use of this handle */
5508c2ecf20Sopenharmony_ci	struct hnae_ae_dev *dev;  /* the device who provides this handle */
5518c2ecf20Sopenharmony_ci	struct phy_device *phy_dev;
5528c2ecf20Sopenharmony_ci	phy_interface_t phy_if;
5538c2ecf20Sopenharmony_ci	u32 if_support;
5548c2ecf20Sopenharmony_ci	int q_num;
5558c2ecf20Sopenharmony_ci	int vf_id;
5568c2ecf20Sopenharmony_ci	unsigned long coal_last_jiffies;
5578c2ecf20Sopenharmony_ci	u32 coal_param;		/* self adapt coalesce param */
5588c2ecf20Sopenharmony_ci	/* the ring index of last ring that set coal param */
5598c2ecf20Sopenharmony_ci	u32 coal_ring_idx;
5608c2ecf20Sopenharmony_ci	u32 eport_id;
5618c2ecf20Sopenharmony_ci	u32 dport_id;	/* v2 tx bd should fill the dport_id */
5628c2ecf20Sopenharmony_ci	bool coal_adapt_en;
5638c2ecf20Sopenharmony_ci	enum hnae_port_type port_type;
5648c2ecf20Sopenharmony_ci	enum hnae_media_type media_type;
5658c2ecf20Sopenharmony_ci	struct list_head node;    /* list to hnae_ae_dev->handle_list */
5668c2ecf20Sopenharmony_ci	struct hnae_buf_ops *bops; /* operation for the buffer */
5678c2ecf20Sopenharmony_ci	struct hnae_queue **qs;  /* array base of all queues */
5688c2ecf20Sopenharmony_ci};
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci#define ring_to_dev(ring) ((ring)->q->dev->dev)
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistruct hnae_handle *hnae_get_handle(struct device *owner_dev,
5738c2ecf20Sopenharmony_ci				    const struct fwnode_handle	*fwnode,
5748c2ecf20Sopenharmony_ci				    u32 port_id,
5758c2ecf20Sopenharmony_ci				    struct hnae_buf_ops *bops);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_civoid hnae_put_handle(struct hnae_handle *handle);
5788c2ecf20Sopenharmony_ciint hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
5798c2ecf20Sopenharmony_civoid hnae_ae_unregister(struct hnae_ae_dev *dev);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ciint hnae_register_notifier(struct notifier_block *nb);
5828c2ecf20Sopenharmony_civoid hnae_unregister_notifier(struct notifier_block *nb);
5838c2ecf20Sopenharmony_ciint hnae_reinit_handle(struct hnae_handle *handle);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci#define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
5868c2ecf20Sopenharmony_ci	(q)->tx_ring.io_base + RCB_REG_TAIL)
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci#ifndef assert
5898c2ecf20Sopenharmony_ci#define assert(cond)
5908c2ecf20Sopenharmony_ci#endif
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
5938c2ecf20Sopenharmony_ci					  struct hnae_desc_cb *cb)
5948c2ecf20Sopenharmony_ci{
5958c2ecf20Sopenharmony_ci	struct hnae_buf_ops *bops = ring->q->handle->bops;
5968c2ecf20Sopenharmony_ci	int ret;
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	ret = bops->alloc_buffer(ring, cb);
5998c2ecf20Sopenharmony_ci	if (ret)
6008c2ecf20Sopenharmony_ci		goto out;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	ret = bops->map_buffer(ring, cb);
6038c2ecf20Sopenharmony_ci	if (ret)
6048c2ecf20Sopenharmony_ci		goto out_with_buf;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	return 0;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ciout_with_buf:
6098c2ecf20Sopenharmony_ci	bops->free_buffer(ring, cb);
6108c2ecf20Sopenharmony_ciout:
6118c2ecf20Sopenharmony_ci	return ret;
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if (ret)
6198c2ecf20Sopenharmony_ci		return ret;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	return 0;
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
6298c2ecf20Sopenharmony_ci	ring->desc[i].addr = 0;
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct hnae_buf_ops *bops = ring->q->handle->bops;
6358c2ecf20Sopenharmony_ci	struct hnae_desc_cb *cb = &ring->desc_cb[i];
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	if (!ring->desc_cb[i].dma)
6388c2ecf20Sopenharmony_ci		return;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	hnae_buffer_detach(ring, i);
6418c2ecf20Sopenharmony_ci	bops->free_buffer(ring, cb);
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci/* detach a in-used buffer and replace with a reserved one  */
6458c2ecf20Sopenharmony_cistatic inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
6468c2ecf20Sopenharmony_ci				       struct hnae_desc_cb *res_cb)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	struct hnae_buf_ops *bops = ring->q->handle->bops;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	bops->unmap_buffer(ring, &ring->desc_cb[i]);
6518c2ecf20Sopenharmony_ci	ring->desc_cb[i] = *res_cb;
6528c2ecf20Sopenharmony_ci	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
6538c2ecf20Sopenharmony_ci	ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
6578c2ecf20Sopenharmony_ci{
6588c2ecf20Sopenharmony_ci	ring->desc_cb[i].reuse_flag = 0;
6598c2ecf20Sopenharmony_ci	ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma
6608c2ecf20Sopenharmony_ci		+ ring->desc_cb[i].page_offset);
6618c2ecf20Sopenharmony_ci	ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci/* when reinit buffer size, we should reinit buffer description */
6658c2ecf20Sopenharmony_cistatic inline void hnae_reinit_all_ring_desc(struct hnae_handle *h)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	int i, j;
6688c2ecf20Sopenharmony_ci	struct hnae_ring *ring;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	for (i = 0; i < h->q_num; i++) {
6718c2ecf20Sopenharmony_ci		ring = &h->qs[i]->rx_ring;
6728c2ecf20Sopenharmony_ci		for (j = 0; j < ring->desc_num; j++)
6738c2ecf20Sopenharmony_ci			ring->desc[j].addr = cpu_to_le64(ring->desc_cb[j].dma);
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	wmb();	/* commit all data before submit */
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci/* when reinit buffer size, we should reinit page offset */
6808c2ecf20Sopenharmony_cistatic inline void hnae_reinit_all_ring_page_off(struct hnae_handle *h)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	int i, j;
6838c2ecf20Sopenharmony_ci	struct hnae_ring *ring;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	for (i = 0; i < h->q_num; i++) {
6868c2ecf20Sopenharmony_ci		ring = &h->qs[i]->rx_ring;
6878c2ecf20Sopenharmony_ci		for (j = 0; j < ring->desc_num; j++) {
6888c2ecf20Sopenharmony_ci			ring->desc_cb[j].page_offset = 0;
6898c2ecf20Sopenharmony_ci			if (ring->desc[j].addr !=
6908c2ecf20Sopenharmony_ci			    cpu_to_le64(ring->desc_cb[j].dma))
6918c2ecf20Sopenharmony_ci				ring->desc[j].addr =
6928c2ecf20Sopenharmony_ci					cpu_to_le64(ring->desc_cb[j].dma);
6938c2ecf20Sopenharmony_ci		}
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	wmb();	/* commit all data before submit */
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci#define hnae_set_field(origin, mask, shift, val) \
7008c2ecf20Sopenharmony_ci	do { \
7018c2ecf20Sopenharmony_ci		(origin) &= (~(mask)); \
7028c2ecf20Sopenharmony_ci		(origin) |= ((val) << (shift)) & (mask); \
7038c2ecf20Sopenharmony_ci	} while (0)
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci#define hnae_set_bit(origin, shift, val) \
7068c2ecf20Sopenharmony_ci	hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci#define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci#define hnae_get_bit(origin, shift) \
7118c2ecf20Sopenharmony_ci	hnae_get_field((origin), (0x1 << (shift)), (shift))
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci#endif
714