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