1 /*
2  * Copyright (C) 2015 Cavium, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  */
8 
9 #ifndef NICVF_QUEUES_H
10 #define NICVF_QUEUES_H
11 
12 #include <linux/netdevice.h>
13 #include "q_struct.h"
14 
15 #define MAX_QUEUE_SET			128
16 #define MAX_RCV_QUEUES_PER_QS		8
17 #define MAX_RCV_BUF_DESC_RINGS_PER_QS	2
18 #define MAX_SND_QUEUES_PER_QS		8
19 #define MAX_CMP_QUEUES_PER_QS		8
20 
21 /* VF's queue interrupt ranges */
22 #define	NICVF_INTR_ID_CQ		0
23 #define	NICVF_INTR_ID_SQ		8
24 #define	NICVF_INTR_ID_RBDR		16
25 #define	NICVF_INTR_ID_MISC		18
26 #define	NICVF_INTR_ID_QS_ERR		19
27 
28 #define	for_each_cq_irq(irq)	\
29 	for (irq = NICVF_INTR_ID_CQ; irq < NICVF_INTR_ID_SQ; irq++)
30 #define	for_each_sq_irq(irq)	\
31 	for (irq = NICVF_INTR_ID_SQ; irq < NICVF_INTR_ID_RBDR; irq++)
32 #define	for_each_rbdr_irq(irq)	\
33 	for (irq = NICVF_INTR_ID_RBDR; irq < NICVF_INTR_ID_MISC; irq++)
34 
35 #define RBDR_SIZE0		0ULL /* 8K entries */
36 #define RBDR_SIZE1		1ULL /* 16K entries */
37 #define RBDR_SIZE2		2ULL /* 32K entries */
38 #define RBDR_SIZE3		3ULL /* 64K entries */
39 #define RBDR_SIZE4		4ULL /* 126K entries */
40 #define RBDR_SIZE5		5ULL /* 256K entries */
41 #define RBDR_SIZE6		6ULL /* 512K entries */
42 
43 #define SND_QUEUE_SIZE0		0ULL /* 1K entries */
44 #define SND_QUEUE_SIZE1		1ULL /* 2K entries */
45 #define SND_QUEUE_SIZE2		2ULL /* 4K entries */
46 #define SND_QUEUE_SIZE3		3ULL /* 8K entries */
47 #define SND_QUEUE_SIZE4		4ULL /* 16K entries */
48 #define SND_QUEUE_SIZE5		5ULL /* 32K entries */
49 #define SND_QUEUE_SIZE6		6ULL /* 64K entries */
50 
51 #define CMP_QUEUE_SIZE0		0ULL /* 1K entries */
52 #define CMP_QUEUE_SIZE1		1ULL /* 2K entries */
53 #define CMP_QUEUE_SIZE2		2ULL /* 4K entries */
54 #define CMP_QUEUE_SIZE3		3ULL /* 8K entries */
55 #define CMP_QUEUE_SIZE4		4ULL /* 16K entries */
56 #define CMP_QUEUE_SIZE5		5ULL /* 32K entries */
57 #define CMP_QUEUE_SIZE6		6ULL /* 64K entries */
58 
59 /* Default queue count per QS, its lengths and threshold values */
60 #define RBDR_CNT		1
61 #define RCV_QUEUE_CNT		8
62 #define SND_QUEUE_CNT		8
63 #define CMP_QUEUE_CNT		8 /* Max of RCV and SND qcount */
64 
65 #define SND_QSIZE		SND_QUEUE_SIZE2
66 #define SND_QUEUE_LEN		(1ULL << (SND_QSIZE + 10))
67 #define MAX_SND_QUEUE_LEN	(1ULL << (SND_QUEUE_SIZE6 + 10))
68 #define SND_QUEUE_THRESH	2ULL
69 #define MIN_SQ_DESC_PER_PKT_XMIT	2
70 /* Since timestamp not enabled, otherwise 2 */
71 #define MAX_CQE_PER_PKT_XMIT		1
72 
73 /* Keep CQ and SQ sizes same, if timestamping
74  * is enabled this equation will change.
75  */
76 #define CMP_QSIZE		CMP_QUEUE_SIZE2
77 #define CMP_QUEUE_LEN		(1ULL << (CMP_QSIZE + 10))
78 #define CMP_QUEUE_CQE_THRESH	0
79 #define CMP_QUEUE_TIMER_THRESH	220 /* 10usec */
80 
81 #define RBDR_SIZE		RBDR_SIZE0
82 #define RCV_BUF_COUNT		(1ULL << (RBDR_SIZE + 13))
83 #define MAX_RCV_BUF_COUNT	(1ULL << (RBDR_SIZE6 + 13))
84 #define RBDR_THRESH		(RCV_BUF_COUNT / 2)
85 #define DMA_BUFFER_LEN		2048 /* In multiples of 128bytes */
86 #define RCV_FRAG_LEN	(SKB_DATA_ALIGN(DMA_BUFFER_LEN + NET_SKB_PAD) + \
87 			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + \
88 			 (NICVF_RCV_BUF_ALIGN_BYTES * 2))
89 #define RCV_DATA_OFFSET		NICVF_RCV_BUF_ALIGN_BYTES
90 
91 #define MAX_CQES_FOR_TX		((SND_QUEUE_LEN / MIN_SQ_DESC_PER_PKT_XMIT) * \
92 				 MAX_CQE_PER_PKT_XMIT)
93 /* Calculate number of CQEs to reserve for all SQEs.
94  * Its 1/256th level of CQ size.
95  * '+ 1' to account for pipelining
96  */
97 #define RQ_CQ_DROP		((256 / (CMP_QUEUE_LEN / \
98 				 (CMP_QUEUE_LEN - MAX_CQES_FOR_TX))) + 1)
99 
100 /* Descriptor size in bytes */
101 #define SND_QUEUE_DESC_SIZE	16
102 #define CMP_QUEUE_DESC_SIZE	512
103 
104 /* Buffer / descriptor alignments */
105 #define NICVF_RCV_BUF_ALIGN		7
106 #define NICVF_RCV_BUF_ALIGN_BYTES	(1ULL << NICVF_RCV_BUF_ALIGN)
107 #define NICVF_CQ_BASE_ALIGN_BYTES	512  /* 9 bits */
108 #define NICVF_SQ_BASE_ALIGN_BYTES	128  /* 7 bits */
109 
110 #define NICVF_ALIGNED_ADDR(ADDR, ALIGN_BYTES)	ALIGN(ADDR, ALIGN_BYTES)
111 #define NICVF_ADDR_ALIGN_LEN(ADDR, BYTES)\
112 	(NICVF_ALIGNED_ADDR(ADDR, BYTES) - BYTES)
113 #define NICVF_RCV_BUF_ALIGN_LEN(X)\
114 	(NICVF_ALIGNED_ADDR(X, NICVF_RCV_BUF_ALIGN_BYTES) - X)
115 
116 /* Queue enable/disable */
117 #define NICVF_SQ_EN		BIT_ULL(19)
118 
119 /* Queue reset */
120 #define NICVF_CQ_RESET		BIT_ULL(41)
121 #define NICVF_SQ_RESET		BIT_ULL(17)
122 #define NICVF_RBDR_RESET	BIT_ULL(43)
123 
124 enum CQ_RX_ERRLVL_E {
125 	CQ_ERRLVL_MAC,
126 	CQ_ERRLVL_L2,
127 	CQ_ERRLVL_L3,
128 	CQ_ERRLVL_L4,
129 };
130 
131 enum CQ_RX_ERROP_E {
132 	CQ_RX_ERROP_RE_NONE = 0x0,
133 	CQ_RX_ERROP_RE_PARTIAL = 0x1,
134 	CQ_RX_ERROP_RE_JABBER = 0x2,
135 	CQ_RX_ERROP_RE_FCS = 0x7,
136 	CQ_RX_ERROP_RE_TERMINATE = 0x9,
137 	CQ_RX_ERROP_RE_RX_CTL = 0xb,
138 	CQ_RX_ERROP_PREL2_ERR = 0x1f,
139 	CQ_RX_ERROP_L2_FRAGMENT = 0x20,
140 	CQ_RX_ERROP_L2_OVERRUN = 0x21,
141 	CQ_RX_ERROP_L2_PFCS = 0x22,
142 	CQ_RX_ERROP_L2_PUNY = 0x23,
143 	CQ_RX_ERROP_L2_MAL = 0x24,
144 	CQ_RX_ERROP_L2_OVERSIZE = 0x25,
145 	CQ_RX_ERROP_L2_UNDERSIZE = 0x26,
146 	CQ_RX_ERROP_L2_LENMISM = 0x27,
147 	CQ_RX_ERROP_L2_PCLP = 0x28,
148 	CQ_RX_ERROP_IP_NOT = 0x41,
149 	CQ_RX_ERROP_IP_CSUM_ERR = 0x42,
150 	CQ_RX_ERROP_IP_MAL = 0x43,
151 	CQ_RX_ERROP_IP_MALD = 0x44,
152 	CQ_RX_ERROP_IP_HOP = 0x45,
153 	CQ_RX_ERROP_L3_ICRC = 0x46,
154 	CQ_RX_ERROP_L3_PCLP = 0x47,
155 	CQ_RX_ERROP_L4_MAL = 0x61,
156 	CQ_RX_ERROP_L4_CHK = 0x62,
157 	CQ_RX_ERROP_UDP_LEN = 0x63,
158 	CQ_RX_ERROP_L4_PORT = 0x64,
159 	CQ_RX_ERROP_TCP_FLAG = 0x65,
160 	CQ_RX_ERROP_TCP_OFFSET = 0x66,
161 	CQ_RX_ERROP_L4_PCLP = 0x67,
162 	CQ_RX_ERROP_RBDR_TRUNC = 0x70,
163 };
164 
165 enum CQ_TX_ERROP_E {
166 	CQ_TX_ERROP_GOOD = 0x0,
167 	CQ_TX_ERROP_DESC_FAULT = 0x10,
168 	CQ_TX_ERROP_HDR_CONS_ERR = 0x11,
169 	CQ_TX_ERROP_SUBDC_ERR = 0x12,
170 	CQ_TX_ERROP_IMM_SIZE_OFLOW = 0x80,
171 	CQ_TX_ERROP_DATA_SEQUENCE_ERR = 0x81,
172 	CQ_TX_ERROP_MEM_SEQUENCE_ERR = 0x82,
173 	CQ_TX_ERROP_LOCK_VIOL = 0x83,
174 	CQ_TX_ERROP_DATA_FAULT = 0x84,
175 	CQ_TX_ERROP_TSTMP_CONFLICT = 0x85,
176 	CQ_TX_ERROP_TSTMP_TIMEOUT = 0x86,
177 	CQ_TX_ERROP_MEM_FAULT = 0x87,
178 	CQ_TX_ERROP_CK_OVERLAP = 0x88,
179 	CQ_TX_ERROP_CK_OFLOW = 0x89,
180 	CQ_TX_ERROP_ENUM_LAST = 0x8a,
181 };
182 
183 struct cmp_queue_stats {
184 	struct rx_stats {
185 		struct {
186 			u64 mac_errs;
187 			u64 l2_errs;
188 			u64 l3_errs;
189 			u64 l4_errs;
190 		} errlvl;
191 		struct {
192 			u64 good;
193 			u64 partial_pkts;
194 			u64 jabber_errs;
195 			u64 fcs_errs;
196 			u64 terminate_errs;
197 			u64 bgx_rx_errs;
198 			u64 prel2_errs;
199 			u64 l2_frags;
200 			u64 l2_overruns;
201 			u64 l2_pfcs;
202 			u64 l2_puny;
203 			u64 l2_hdr_malformed;
204 			u64 l2_oversize;
205 			u64 l2_undersize;
206 			u64 l2_len_mismatch;
207 			u64 l2_pclp;
208 			u64 non_ip;
209 			u64 ip_csum_err;
210 			u64 ip_hdr_malformed;
211 			u64 ip_payload_malformed;
212 			u64 ip_hop_errs;
213 			u64 l3_icrc_errs;
214 			u64 l3_pclp;
215 			u64 l4_malformed;
216 			u64 l4_csum_errs;
217 			u64 udp_len_err;
218 			u64 bad_l4_port;
219 			u64 bad_tcp_flag;
220 			u64 tcp_offset_errs;
221 			u64 l4_pclp;
222 			u64 pkt_truncated;
223 		} errop;
224 	} rx;
225 	struct tx_stats {
226 		u64 good;
227 		u64 desc_fault;
228 		u64 hdr_cons_err;
229 		u64 subdesc_err;
230 		u64 imm_size_oflow;
231 		u64 data_seq_err;
232 		u64 mem_seq_err;
233 		u64 lock_viol;
234 		u64 data_fault;
235 		u64 tstmp_conflict;
236 		u64 tstmp_timeout;
237 		u64 mem_fault;
238 		u64 csum_overlap;
239 		u64 csum_overflow;
240 	} tx;
241 } ____cacheline_aligned_in_smp;
242 
243 enum RQ_SQ_STATS {
244 	RQ_SQ_STATS_OCTS,
245 	RQ_SQ_STATS_PKTS,
246 };
247 
248 struct rx_tx_queue_stats {
249 	u64	bytes;
250 	u64	pkts;
251 } ____cacheline_aligned_in_smp;
252 
253 struct q_desc_mem {
254 	dma_addr_t	dma;
255 	u64		size;
256 	u16		q_len;
257 	dma_addr_t	phys_base;
258 	void		*base;
259 	void		*unalign_base;
260 };
261 
262 struct rbdr {
263 	bool		enable;
264 	u32		dma_size;
265 	u32		frag_len;
266 	u32		thresh;		/* Threshold level for interrupt */
267 	void		*desc;
268 	u32		head;
269 	u32		tail;
270 	struct q_desc_mem   dmem;
271 } ____cacheline_aligned_in_smp;
272 
273 struct rcv_queue {
274 	bool		enable;
275 	struct	rbdr	*rbdr_start;
276 	struct	rbdr	*rbdr_cont;
277 	bool		en_tcp_reassembly;
278 	u8		cq_qs;  /* CQ's QS to which this RQ is assigned */
279 	u8		cq_idx; /* CQ index (0 to 7) in the QS */
280 	u8		cont_rbdr_qs;      /* Continue buffer ptrs - QS num */
281 	u8		cont_qs_rbdr_idx;  /* RBDR idx in the cont QS */
282 	u8		start_rbdr_qs;     /* First buffer ptrs - QS num */
283 	u8		start_qs_rbdr_idx; /* RBDR idx in the above QS */
284 	u8		caching;
285 	struct		rx_tx_queue_stats stats;
286 } ____cacheline_aligned_in_smp;
287 
288 struct cmp_queue {
289 	bool		enable;
290 	u16		thresh;
291 	spinlock_t	lock;  /* lock to serialize processing CQEs */
292 	void		*desc;
293 	struct q_desc_mem   dmem;
294 	struct cmp_queue_stats	stats;
295 } ____cacheline_aligned_in_smp;
296 
297 struct snd_queue {
298 	bool		enable;
299 	u8		cq_qs;  /* CQ's QS to which this SQ is pointing */
300 	u8		cq_idx; /* CQ index (0 to 7) in the above QS */
301 	u16		thresh;
302 	atomic_t	free_cnt;
303 	u32		head;
304 	u32		tail;
305 	u64		*skbuff;
306 	void		*desc;
307 
308 #define	TSO_HEADER_SIZE	128
309 	/* For TSO segment's header */
310 	char		*tso_hdrs;
311 	dma_addr_t	tso_hdrs_phys;
312 
313 	cpumask_t	affinity_mask;
314 	struct q_desc_mem   dmem;
315 	struct rx_tx_queue_stats stats;
316 } ____cacheline_aligned_in_smp;
317 
318 struct queue_set {
319 	bool		enable;
320 	bool		be_en;
321 	u8		vnic_id;
322 	u8		rq_cnt;
323 	u8		cq_cnt;
324 	u64		cq_len;
325 	u8		sq_cnt;
326 	u64		sq_len;
327 	u8		rbdr_cnt;
328 	u64		rbdr_len;
329 	struct	rcv_queue	rq[MAX_RCV_QUEUES_PER_QS];
330 	struct	cmp_queue	cq[MAX_CMP_QUEUES_PER_QS];
331 	struct	snd_queue	sq[MAX_SND_QUEUES_PER_QS];
332 	struct	rbdr		rbdr[MAX_RCV_BUF_DESC_RINGS_PER_QS];
333 } ____cacheline_aligned_in_smp;
334 
335 #define GET_RBDR_DESC(RING, idx)\
336 		(&(((struct rbdr_entry_t *)((RING)->desc))[idx]))
337 #define GET_SQ_DESC(RING, idx)\
338 		(&(((struct sq_hdr_subdesc *)((RING)->desc))[idx]))
339 #define GET_CQ_DESC(RING, idx)\
340 		(&(((union cq_desc_t *)((RING)->desc))[idx]))
341 
342 /* CQ status bits */
343 #define	CQ_WR_FULL	BIT(26)
344 #define	CQ_WR_DISABLE	BIT(25)
345 #define	CQ_WR_FAULT	BIT(24)
346 #define	CQ_CQE_COUNT	(0xFFFF << 0)
347 
348 #define	CQ_ERR_MASK	(CQ_WR_FULL | CQ_WR_DISABLE | CQ_WR_FAULT)
349 
350 int nicvf_set_qset_resources(struct nicvf *nic);
351 int nicvf_config_data_transfer(struct nicvf *nic, bool enable);
352 void nicvf_qset_config(struct nicvf *nic, bool enable);
353 void nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
354 			    int qidx, bool enable);
355 
356 void nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx);
357 void nicvf_sq_disable(struct nicvf *nic, int qidx);
358 void nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt);
359 void nicvf_sq_free_used_descs(struct net_device *netdev,
360 			      struct snd_queue *sq, int qidx);
361 int nicvf_sq_append_skb(struct nicvf *nic, struct sk_buff *skb);
362 
363 struct sk_buff *nicvf_get_rcv_skb(struct nicvf *nic, struct cqe_rx_t *cqe_rx);
364 void nicvf_rbdr_task(unsigned long data);
365 void nicvf_rbdr_work(struct work_struct *work);
366 
367 void nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx);
368 void nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx);
369 void nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx);
370 int nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx);
371 
372 /* Register access APIs */
373 void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val);
374 u64  nicvf_reg_read(struct nicvf *nic, u64 offset);
375 void nicvf_qset_reg_write(struct nicvf *nic, u64 offset, u64 val);
376 u64 nicvf_qset_reg_read(struct nicvf *nic, u64 offset);
377 void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
378 			   u64 qidx, u64 val);
379 u64  nicvf_queue_reg_read(struct nicvf *nic,
380 			  u64 offset, u64 qidx);
381 
382 /* Stats */
383 void nicvf_update_rq_stats(struct nicvf *nic, int rq_idx);
384 void nicvf_update_sq_stats(struct nicvf *nic, int sq_idx);
385 int nicvf_check_cqe_rx_errs(struct nicvf *nic,
386 			    struct cmp_queue *cq, struct cqe_rx_t *cqe_rx);
387 int nicvf_check_cqe_tx_errs(struct nicvf *nic,
388 			    struct cmp_queue *cq, struct cqe_send_t *cqe_tx);
389 #endif /* NICVF_QUEUES_H */
390