162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/* Atlantic Network Driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 2014-2019 aQuantia Corporation
562306a36Sopenharmony_ci * Copyright (C) 2019-2020 Marvell International Ltd.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci/* File aq_ring.h: Declaration of functions for Rx/Tx rings. */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef AQ_RING_H
1162306a36Sopenharmony_ci#define AQ_RING_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include "aq_common.h"
1462306a36Sopenharmony_ci#include "aq_vec.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define AQ_XDP_HEADROOM		ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8)
1762306a36Sopenharmony_ci#define AQ_XDP_TAILROOM		SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct page;
2062306a36Sopenharmony_cistruct aq_nic_cfg_s;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct aq_rxpage {
2362306a36Sopenharmony_ci	struct page *page;
2462306a36Sopenharmony_ci	dma_addr_t daddr;
2562306a36Sopenharmony_ci	unsigned int order;
2662306a36Sopenharmony_ci	unsigned int pg_off;
2762306a36Sopenharmony_ci};
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*           TxC       SOP        DX         EOP
3062306a36Sopenharmony_ci *         +----------+----------+----------+-----------
3162306a36Sopenharmony_ci *   8bytes|len l3,l4 | pa       | pa       | pa
3262306a36Sopenharmony_ci *         +----------+----------+----------+-----------
3362306a36Sopenharmony_ci * 4/8bytes|len pkt   |len pkt   |          | skb
3462306a36Sopenharmony_ci *         +----------+----------+----------+-----------
3562306a36Sopenharmony_ci * 4/8bytes|is_gso    |len,flags |len       |len,is_eop
3662306a36Sopenharmony_ci *         +----------+----------+----------+-----------
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci *  This aq_ring_buff_s doesn't have endianness dependency.
3962306a36Sopenharmony_ci *  It is __packed for cache line optimizations.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_cistruct __packed aq_ring_buff_s {
4262306a36Sopenharmony_ci	union {
4362306a36Sopenharmony_ci		/* RX/TX */
4462306a36Sopenharmony_ci		dma_addr_t pa;
4562306a36Sopenharmony_ci		/* RX */
4662306a36Sopenharmony_ci		struct {
4762306a36Sopenharmony_ci			u32 rss_hash;
4862306a36Sopenharmony_ci			u16 next;
4962306a36Sopenharmony_ci			u8 is_hash_l4;
5062306a36Sopenharmony_ci			u8 rsvd1;
5162306a36Sopenharmony_ci			struct aq_rxpage rxdata;
5262306a36Sopenharmony_ci			u16 vlan_rx_tag;
5362306a36Sopenharmony_ci		};
5462306a36Sopenharmony_ci		/* EOP */
5562306a36Sopenharmony_ci		struct {
5662306a36Sopenharmony_ci			dma_addr_t pa_eop;
5762306a36Sopenharmony_ci			struct sk_buff *skb;
5862306a36Sopenharmony_ci			struct xdp_frame *xdpf;
5962306a36Sopenharmony_ci		};
6062306a36Sopenharmony_ci		/* TxC */
6162306a36Sopenharmony_ci		struct {
6262306a36Sopenharmony_ci			u32 mss;
6362306a36Sopenharmony_ci			u8 len_l2;
6462306a36Sopenharmony_ci			u8 len_l3;
6562306a36Sopenharmony_ci			u8 len_l4;
6662306a36Sopenharmony_ci			u8 is_ipv6:1;
6762306a36Sopenharmony_ci			u8 rsvd2:7;
6862306a36Sopenharmony_ci			u32 len_pkt;
6962306a36Sopenharmony_ci			u16 vlan_tx_tag;
7062306a36Sopenharmony_ci		};
7162306a36Sopenharmony_ci	};
7262306a36Sopenharmony_ci	union {
7362306a36Sopenharmony_ci		struct {
7462306a36Sopenharmony_ci			u32 len:16;
7562306a36Sopenharmony_ci			u32 is_ip_cso:1;
7662306a36Sopenharmony_ci			u32 is_udp_cso:1;
7762306a36Sopenharmony_ci			u32 is_tcp_cso:1;
7862306a36Sopenharmony_ci			u32 is_cso_err:1;
7962306a36Sopenharmony_ci			u32 is_sop:1;
8062306a36Sopenharmony_ci			u32 is_eop:1;
8162306a36Sopenharmony_ci			u32 is_gso_tcp:1;
8262306a36Sopenharmony_ci			u32 is_gso_udp:1;
8362306a36Sopenharmony_ci			u32 is_mapped:1;
8462306a36Sopenharmony_ci			u32 is_cleaned:1;
8562306a36Sopenharmony_ci			u32 is_error:1;
8662306a36Sopenharmony_ci			u32 is_vlan:1;
8762306a36Sopenharmony_ci			u32 is_lro:1;
8862306a36Sopenharmony_ci			u32 rsvd3:3;
8962306a36Sopenharmony_ci			u16 eop_index;
9062306a36Sopenharmony_ci			u16 rsvd4;
9162306a36Sopenharmony_ci		};
9262306a36Sopenharmony_ci		u64 flags;
9362306a36Sopenharmony_ci	};
9462306a36Sopenharmony_ci};
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistruct aq_ring_stats_rx_s {
9762306a36Sopenharmony_ci	struct u64_stats_sync syncp;	/* must be first */
9862306a36Sopenharmony_ci	u64 errors;
9962306a36Sopenharmony_ci	u64 packets;
10062306a36Sopenharmony_ci	u64 bytes;
10162306a36Sopenharmony_ci	u64 lro_packets;
10262306a36Sopenharmony_ci	u64 jumbo_packets;
10362306a36Sopenharmony_ci	u64 alloc_fails;
10462306a36Sopenharmony_ci	u64 skb_alloc_fails;
10562306a36Sopenharmony_ci	u64 polls;
10662306a36Sopenharmony_ci	u64 pg_losts;
10762306a36Sopenharmony_ci	u64 pg_flips;
10862306a36Sopenharmony_ci	u64 pg_reuses;
10962306a36Sopenharmony_ci	u64 xdp_aborted;
11062306a36Sopenharmony_ci	u64 xdp_drop;
11162306a36Sopenharmony_ci	u64 xdp_pass;
11262306a36Sopenharmony_ci	u64 xdp_tx;
11362306a36Sopenharmony_ci	u64 xdp_invalid;
11462306a36Sopenharmony_ci	u64 xdp_redirect;
11562306a36Sopenharmony_ci};
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistruct aq_ring_stats_tx_s {
11862306a36Sopenharmony_ci	struct u64_stats_sync syncp;	/* must be first */
11962306a36Sopenharmony_ci	u64 errors;
12062306a36Sopenharmony_ci	u64 packets;
12162306a36Sopenharmony_ci	u64 bytes;
12262306a36Sopenharmony_ci	u64 queue_restarts;
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciunion aq_ring_stats_s {
12662306a36Sopenharmony_ci	struct aq_ring_stats_rx_s rx;
12762306a36Sopenharmony_ci	struct aq_ring_stats_tx_s tx;
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cienum atl_ring_type {
13162306a36Sopenharmony_ci	ATL_RING_TX,
13262306a36Sopenharmony_ci	ATL_RING_RX,
13362306a36Sopenharmony_ci};
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistruct aq_ring_s {
13662306a36Sopenharmony_ci	struct aq_ring_buff_s *buff_ring;
13762306a36Sopenharmony_ci	u8 *dx_ring;		/* descriptors ring, dma shared mem */
13862306a36Sopenharmony_ci	struct aq_nic_s *aq_nic;
13962306a36Sopenharmony_ci	unsigned int idx;	/* for HW layer registers operations */
14062306a36Sopenharmony_ci	unsigned int hw_head;
14162306a36Sopenharmony_ci	unsigned int sw_head;
14262306a36Sopenharmony_ci	unsigned int sw_tail;
14362306a36Sopenharmony_ci	unsigned int size;	/* descriptors number */
14462306a36Sopenharmony_ci	unsigned int dx_size;	/* TX or RX descriptor size,  */
14562306a36Sopenharmony_ci				/* stored here for fater math */
14662306a36Sopenharmony_ci	u16 page_order;
14762306a36Sopenharmony_ci	u16 page_offset;
14862306a36Sopenharmony_ci	u16 frame_max;
14962306a36Sopenharmony_ci	u16 tail_size;
15062306a36Sopenharmony_ci	union aq_ring_stats_s stats;
15162306a36Sopenharmony_ci	dma_addr_t dx_ring_pa;
15262306a36Sopenharmony_ci	struct bpf_prog *xdp_prog;
15362306a36Sopenharmony_ci	enum atl_ring_type ring_type;
15462306a36Sopenharmony_ci	struct xdp_rxq_info xdp_rxq;
15562306a36Sopenharmony_ci};
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistruct aq_ring_param_s {
15862306a36Sopenharmony_ci	unsigned int vec_idx;
15962306a36Sopenharmony_ci	unsigned int cpu;
16062306a36Sopenharmony_ci	cpumask_t affinity_mask;
16162306a36Sopenharmony_ci};
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic inline void *aq_buf_vaddr(struct aq_rxpage *rxpage)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	return page_to_virt(rxpage->page) + rxpage->pg_off;
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic inline dma_addr_t aq_buf_daddr(struct aq_rxpage *rxpage)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	return rxpage->daddr + rxpage->pg_off;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cistatic inline unsigned int aq_ring_next_dx(struct aq_ring_s *self,
17462306a36Sopenharmony_ci					   unsigned int dx)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	return (++dx >= self->size) ? 0U : dx;
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic inline unsigned int aq_ring_avail_dx(struct aq_ring_s *self)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	return (((self->sw_tail >= self->sw_head)) ?
18262306a36Sopenharmony_ci		(self->size - 1) - self->sw_tail + self->sw_head :
18362306a36Sopenharmony_ci		self->sw_head - self->sw_tail - 1);
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ciint aq_ring_tx_alloc(struct aq_ring_s *self,
18762306a36Sopenharmony_ci		     struct aq_nic_s *aq_nic,
18862306a36Sopenharmony_ci		     unsigned int idx,
18962306a36Sopenharmony_ci		     struct aq_nic_cfg_s *aq_nic_cfg);
19062306a36Sopenharmony_ciint aq_ring_rx_alloc(struct aq_ring_s *self,
19162306a36Sopenharmony_ci		     struct aq_nic_s *aq_nic,
19262306a36Sopenharmony_ci		     unsigned int idx,
19362306a36Sopenharmony_ci		     struct aq_nic_cfg_s *aq_nic_cfg);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciint aq_ring_init(struct aq_ring_s *self, const enum atl_ring_type ring_type);
19662306a36Sopenharmony_civoid aq_ring_rx_deinit(struct aq_ring_s *self);
19762306a36Sopenharmony_civoid aq_ring_free(struct aq_ring_s *self);
19862306a36Sopenharmony_civoid aq_ring_update_queue_state(struct aq_ring_s *ring);
19962306a36Sopenharmony_civoid aq_ring_queue_wake(struct aq_ring_s *ring);
20062306a36Sopenharmony_civoid aq_ring_queue_stop(struct aq_ring_s *ring);
20162306a36Sopenharmony_cibool aq_ring_tx_clean(struct aq_ring_s *self);
20262306a36Sopenharmony_ciint aq_xdp_xmit(struct net_device *dev, int num_frames,
20362306a36Sopenharmony_ci		struct xdp_frame **frames, u32 flags);
20462306a36Sopenharmony_ciint aq_ring_rx_clean(struct aq_ring_s *self,
20562306a36Sopenharmony_ci		     struct napi_struct *napi,
20662306a36Sopenharmony_ci		     int *work_done,
20762306a36Sopenharmony_ci		     int budget);
20862306a36Sopenharmony_ciint aq_ring_rx_fill(struct aq_ring_s *self);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciint aq_ring_hwts_rx_alloc(struct aq_ring_s *self,
21162306a36Sopenharmony_ci			  struct aq_nic_s *aq_nic, unsigned int idx,
21262306a36Sopenharmony_ci			  unsigned int size, unsigned int dx_size);
21362306a36Sopenharmony_civoid aq_ring_hwts_rx_free(struct aq_ring_s *self);
21462306a36Sopenharmony_civoid aq_ring_hwts_rx_clean(struct aq_ring_s *self, struct aq_nic_s *aq_nic);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciunsigned int aq_ring_fill_stats_data(struct aq_ring_s *self, u64 *data);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci#endif /* AQ_RING_H */
219