1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * aQuantia Corporation Network Driver
4  * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
5  */
6 
7 /* File aq_ring.h: Declaration of functions for Rx/Tx rings. */
8 
9 #ifndef AQ_RING_H
10 #define AQ_RING_H
11 
12 #include "aq_common.h"
13 
14 struct page;
15 struct aq_nic_cfg_s;
16 
17 struct aq_rxpage {
18 	struct page *page;
19 	dma_addr_t daddr;
20 	unsigned int order;
21 	unsigned int pg_off;
22 };
23 
24 /*           TxC       SOP        DX         EOP
25  *         +----------+----------+----------+-----------
26  *   8bytes|len l3,l4 | pa       | pa       | pa
27  *         +----------+----------+----------+-----------
28  * 4/8bytes|len pkt   |len pkt   |          | skb
29  *         +----------+----------+----------+-----------
30  * 4/8bytes|is_txc    |len,flags |len       |len,is_eop
31  *         +----------+----------+----------+-----------
32  *
33  *  This aq_ring_buff_s doesn't have endianness dependency.
34  *  It is __packed for cache line optimizations.
35  */
36 struct __packed aq_ring_buff_s {
37 	union {
38 		/* RX/TX */
39 		dma_addr_t pa;
40 		/* RX */
41 		struct {
42 			u32 rss_hash;
43 			u16 next;
44 			u8 is_hash_l4;
45 			u8 rsvd1;
46 			struct aq_rxpage rxdata;
47 		};
48 		/* EOP */
49 		struct {
50 			dma_addr_t pa_eop;
51 			struct sk_buff *skb;
52 		};
53 		/* TxC */
54 		struct {
55 			u32 mss;
56 			u8 len_l2;
57 			u8 len_l3;
58 			u8 len_l4;
59 			u8 is_ipv6:1;
60 			u8 rsvd2:7;
61 			u32 len_pkt;
62 		};
63 	};
64 	union {
65 		struct {
66 			u16 len;
67 			u32 is_ip_cso:1;
68 			u32 is_udp_cso:1;
69 			u32 is_tcp_cso:1;
70 			u32 is_cso_err:1;
71 			u32 is_sop:1;
72 			u32 is_eop:1;
73 			u32 is_txc:1;
74 			u32 is_mapped:1;
75 			u32 is_cleaned:1;
76 			u32 is_error:1;
77 			u32 rsvd3:6;
78 			u16 eop_index;
79 			u16 rsvd4;
80 		};
81 		u64 flags;
82 	};
83 };
84 
85 struct aq_ring_stats_rx_s {
86 	u64 errors;
87 	u64 packets;
88 	u64 bytes;
89 	u64 lro_packets;
90 	u64 jumbo_packets;
91 	u64 pg_losts;
92 	u64 pg_flips;
93 	u64 pg_reuses;
94 };
95 
96 struct aq_ring_stats_tx_s {
97 	u64 errors;
98 	u64 packets;
99 	u64 bytes;
100 	u64 queue_restarts;
101 };
102 
103 union aq_ring_stats_s {
104 	struct aq_ring_stats_rx_s rx;
105 	struct aq_ring_stats_tx_s tx;
106 };
107 
108 struct aq_ring_s {
109 	struct aq_ring_buff_s *buff_ring;
110 	u8 *dx_ring;		/* descriptors ring, dma shared mem */
111 	struct aq_nic_s *aq_nic;
112 	unsigned int idx;	/* for HW layer registers operations */
113 	unsigned int hw_head;
114 	unsigned int sw_head;
115 	unsigned int sw_tail;
116 	unsigned int size;	/* descriptors number */
117 	unsigned int dx_size;	/* TX or RX descriptor size,  */
118 				/* stored here for fater math */
119 	unsigned int page_order;
120 	union aq_ring_stats_s stats;
121 	dma_addr_t dx_ring_pa;
122 };
123 
124 struct aq_ring_param_s {
125 	unsigned int vec_idx;
126 	unsigned int cpu;
127 	cpumask_t affinity_mask;
128 };
129 
130 static inline void *aq_buf_vaddr(struct aq_rxpage *rxpage)
131 {
132 	return page_to_virt(rxpage->page) + rxpage->pg_off;
133 }
134 
135 static inline dma_addr_t aq_buf_daddr(struct aq_rxpage *rxpage)
136 {
137 	return rxpage->daddr + rxpage->pg_off;
138 }
139 
140 static inline unsigned int aq_ring_next_dx(struct aq_ring_s *self,
141 					   unsigned int dx)
142 {
143 	return (++dx >= self->size) ? 0U : dx;
144 }
145 
146 static inline unsigned int aq_ring_avail_dx(struct aq_ring_s *self)
147 {
148 	return (((self->sw_tail >= self->sw_head)) ?
149 		(self->size - 1) - self->sw_tail + self->sw_head :
150 		self->sw_head - self->sw_tail - 1);
151 }
152 
153 struct aq_ring_s *aq_ring_tx_alloc(struct aq_ring_s *self,
154 				   struct aq_nic_s *aq_nic,
155 				   unsigned int idx,
156 				   struct aq_nic_cfg_s *aq_nic_cfg);
157 struct aq_ring_s *aq_ring_rx_alloc(struct aq_ring_s *self,
158 				   struct aq_nic_s *aq_nic,
159 				   unsigned int idx,
160 				   struct aq_nic_cfg_s *aq_nic_cfg);
161 int aq_ring_init(struct aq_ring_s *self);
162 void aq_ring_rx_deinit(struct aq_ring_s *self);
163 void aq_ring_free(struct aq_ring_s *self);
164 void aq_ring_update_queue_state(struct aq_ring_s *ring);
165 void aq_ring_queue_wake(struct aq_ring_s *ring);
166 void aq_ring_queue_stop(struct aq_ring_s *ring);
167 bool aq_ring_tx_clean(struct aq_ring_s *self);
168 int aq_ring_rx_clean(struct aq_ring_s *self,
169 		     struct napi_struct *napi,
170 		     int *work_done,
171 		     int budget);
172 int aq_ring_rx_fill(struct aq_ring_s *self);
173 
174 #endif /* AQ_RING_H */
175