1*e6550b3eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2a6a5580cSJeff Kirsher /*
3a6a5580cSJeff Kirsher  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
4a6a5580cSJeff Kirsher  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
5a6a5580cSJeff Kirsher  */
6a6a5580cSJeff Kirsher 
7a6a5580cSJeff Kirsher #ifndef _VNIC_RQ_H_
8a6a5580cSJeff Kirsher #define _VNIC_RQ_H_
9a6a5580cSJeff Kirsher 
10a6a5580cSJeff Kirsher #include <linux/pci.h>
11f586a336SGovindarajulu Varadarajan #include <linux/netdevice.h>
12a6a5580cSJeff Kirsher 
13a6a5580cSJeff Kirsher #include "vnic_dev.h"
14a6a5580cSJeff Kirsher #include "vnic_cq.h"
15a6a5580cSJeff Kirsher 
16a6a5580cSJeff Kirsher /* Receive queue control */
17a6a5580cSJeff Kirsher struct vnic_rq_ctrl {
18a6a5580cSJeff Kirsher 	u64 ring_base;			/* 0x00 */
19a6a5580cSJeff Kirsher 	u32 ring_size;			/* 0x08 */
20a6a5580cSJeff Kirsher 	u32 pad0;
21a6a5580cSJeff Kirsher 	u32 posted_index;		/* 0x10 */
22a6a5580cSJeff Kirsher 	u32 pad1;
23a6a5580cSJeff Kirsher 	u32 cq_index;			/* 0x18 */
24a6a5580cSJeff Kirsher 	u32 pad2;
25a6a5580cSJeff Kirsher 	u32 enable;			/* 0x20 */
26a6a5580cSJeff Kirsher 	u32 pad3;
27a6a5580cSJeff Kirsher 	u32 running;			/* 0x28 */
28a6a5580cSJeff Kirsher 	u32 pad4;
29a6a5580cSJeff Kirsher 	u32 fetch_index;		/* 0x30 */
30a6a5580cSJeff Kirsher 	u32 pad5;
31a6a5580cSJeff Kirsher 	u32 error_interrupt_enable;	/* 0x38 */
32a6a5580cSJeff Kirsher 	u32 pad6;
33a6a5580cSJeff Kirsher 	u32 error_interrupt_offset;	/* 0x40 */
34a6a5580cSJeff Kirsher 	u32 pad7;
35a6a5580cSJeff Kirsher 	u32 error_status;		/* 0x48 */
36a6a5580cSJeff Kirsher 	u32 pad8;
37a6a5580cSJeff Kirsher 	u32 dropped_packet_count;	/* 0x50 */
38a6a5580cSJeff Kirsher 	u32 pad9;
39a6a5580cSJeff Kirsher 	u32 dropped_packet_count_rc;	/* 0x58 */
40a6a5580cSJeff Kirsher 	u32 pad10;
41a6a5580cSJeff Kirsher };
42a6a5580cSJeff Kirsher 
43a6a5580cSJeff Kirsher /* Break the vnic_rq_buf allocations into blocks of 32/64 entries */
44a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_MIN_BLK_ENTRIES 32
45a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_DFLT_BLK_ENTRIES 64
46a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_BLK_ENTRIES(entries) \
47a6a5580cSJeff Kirsher 	((unsigned int)((entries < VNIC_RQ_BUF_DFLT_BLK_ENTRIES) ? \
48a6a5580cSJeff Kirsher 	VNIC_RQ_BUF_MIN_BLK_ENTRIES : VNIC_RQ_BUF_DFLT_BLK_ENTRIES))
49a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_BLK_SZ(entries) \
50a6a5580cSJeff Kirsher 	(VNIC_RQ_BUF_BLK_ENTRIES(entries) * sizeof(struct vnic_rq_buf))
51a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_BLKS_NEEDED(entries) \
52a6a5580cSJeff Kirsher 	DIV_ROUND_UP(entries, VNIC_RQ_BUF_BLK_ENTRIES(entries))
53a6a5580cSJeff Kirsher #define VNIC_RQ_BUF_BLKS_MAX VNIC_RQ_BUF_BLKS_NEEDED(4096)
54a6a5580cSJeff Kirsher 
55a6a5580cSJeff Kirsher struct vnic_rq_buf {
56a6a5580cSJeff Kirsher 	struct vnic_rq_buf *next;
57a6a5580cSJeff Kirsher 	dma_addr_t dma_addr;
58a6a5580cSJeff Kirsher 	void *os_buf;
59a6a5580cSJeff Kirsher 	unsigned int os_buf_index;
60a6a5580cSJeff Kirsher 	unsigned int len;
61a6a5580cSJeff Kirsher 	unsigned int index;
62a6a5580cSJeff Kirsher 	void *desc;
6392e2b469SNeel Patel 	uint64_t wr_id;
64a6a5580cSJeff Kirsher };
65a6a5580cSJeff Kirsher 
66f586a336SGovindarajulu Varadarajan enum enic_poll_state {
67f586a336SGovindarajulu Varadarajan 	ENIC_POLL_STATE_IDLE,
68f586a336SGovindarajulu Varadarajan 	ENIC_POLL_STATE_NAPI,
69f586a336SGovindarajulu Varadarajan 	ENIC_POLL_STATE_POLL
70f586a336SGovindarajulu Varadarajan };
71f586a336SGovindarajulu Varadarajan 
72a6a5580cSJeff Kirsher struct vnic_rq {
73a6a5580cSJeff Kirsher 	unsigned int index;
74a6a5580cSJeff Kirsher 	struct vnic_dev *vdev;
75a6a5580cSJeff Kirsher 	struct vnic_rq_ctrl __iomem *ctrl;              /* memory-mapped */
76a6a5580cSJeff Kirsher 	struct vnic_dev_ring ring;
77a6a5580cSJeff Kirsher 	struct vnic_rq_buf *bufs[VNIC_RQ_BUF_BLKS_MAX];
78a6a5580cSJeff Kirsher 	struct vnic_rq_buf *to_use;
79a6a5580cSJeff Kirsher 	struct vnic_rq_buf *to_clean;
80a6a5580cSJeff Kirsher 	void *os_buf_head;
81a6a5580cSJeff Kirsher 	unsigned int pkts_outstanding;
82a6a5580cSJeff Kirsher };
83a6a5580cSJeff Kirsher 
vnic_rq_desc_avail(struct vnic_rq * rq)84a6a5580cSJeff Kirsher static inline unsigned int vnic_rq_desc_avail(struct vnic_rq *rq)
85a6a5580cSJeff Kirsher {
86a6a5580cSJeff Kirsher 	/* how many does SW own? */
87a6a5580cSJeff Kirsher 	return rq->ring.desc_avail;
88a6a5580cSJeff Kirsher }
89a6a5580cSJeff Kirsher 
vnic_rq_desc_used(struct vnic_rq * rq)90a6a5580cSJeff Kirsher static inline unsigned int vnic_rq_desc_used(struct vnic_rq *rq)
91a6a5580cSJeff Kirsher {
92a6a5580cSJeff Kirsher 	/* how many does HW own? */
93a6a5580cSJeff Kirsher 	return rq->ring.desc_count - rq->ring.desc_avail - 1;
94a6a5580cSJeff Kirsher }
95a6a5580cSJeff Kirsher 
vnic_rq_next_desc(struct vnic_rq * rq)96a6a5580cSJeff Kirsher static inline void *vnic_rq_next_desc(struct vnic_rq *rq)
97a6a5580cSJeff Kirsher {
98a6a5580cSJeff Kirsher 	return rq->to_use->desc;
99a6a5580cSJeff Kirsher }
100a6a5580cSJeff Kirsher 
vnic_rq_next_index(struct vnic_rq * rq)101a6a5580cSJeff Kirsher static inline unsigned int vnic_rq_next_index(struct vnic_rq *rq)
102a6a5580cSJeff Kirsher {
103a6a5580cSJeff Kirsher 	return rq->to_use->index;
104a6a5580cSJeff Kirsher }
105a6a5580cSJeff Kirsher 
vnic_rq_post(struct vnic_rq * rq,void * os_buf,unsigned int os_buf_index,dma_addr_t dma_addr,unsigned int len,uint64_t wrid)106a6a5580cSJeff Kirsher static inline void vnic_rq_post(struct vnic_rq *rq,
107a6a5580cSJeff Kirsher 	void *os_buf, unsigned int os_buf_index,
10892e2b469SNeel Patel 	dma_addr_t dma_addr, unsigned int len,
10992e2b469SNeel Patel 	uint64_t wrid)
110a6a5580cSJeff Kirsher {
111a6a5580cSJeff Kirsher 	struct vnic_rq_buf *buf = rq->to_use;
112a6a5580cSJeff Kirsher 
113a6a5580cSJeff Kirsher 	buf->os_buf = os_buf;
114a6a5580cSJeff Kirsher 	buf->os_buf_index = os_buf_index;
115a6a5580cSJeff Kirsher 	buf->dma_addr = dma_addr;
116a6a5580cSJeff Kirsher 	buf->len = len;
11792e2b469SNeel Patel 	buf->wr_id = wrid;
118a6a5580cSJeff Kirsher 
119a6a5580cSJeff Kirsher 	buf = buf->next;
120a6a5580cSJeff Kirsher 	rq->to_use = buf;
121a6a5580cSJeff Kirsher 	rq->ring.desc_avail--;
122a6a5580cSJeff Kirsher 
123a6a5580cSJeff Kirsher 	/* Move the posted_index every nth descriptor
124a6a5580cSJeff Kirsher 	 */
125a6a5580cSJeff Kirsher 
126a6a5580cSJeff Kirsher #ifndef VNIC_RQ_RETURN_RATE
127a6a5580cSJeff Kirsher #define VNIC_RQ_RETURN_RATE		0xf	/* keep 2^n - 1 */
128a6a5580cSJeff Kirsher #endif
129a6a5580cSJeff Kirsher 
130a6a5580cSJeff Kirsher 	if ((buf->index & VNIC_RQ_RETURN_RATE) == 0) {
131a6a5580cSJeff Kirsher 		/* Adding write memory barrier prevents compiler and/or CPU
132a6a5580cSJeff Kirsher 		 * reordering, thus avoiding descriptor posting before
133a6a5580cSJeff Kirsher 		 * descriptor is initialized. Otherwise, hardware can read
134a6a5580cSJeff Kirsher 		 * stale descriptor fields.
135a6a5580cSJeff Kirsher 		 */
136a6a5580cSJeff Kirsher 		wmb();
137a6a5580cSJeff Kirsher 		iowrite32(buf->index, &rq->ctrl->posted_index);
138a6a5580cSJeff Kirsher 	}
139a6a5580cSJeff Kirsher }
140a6a5580cSJeff Kirsher 
vnic_rq_return_descs(struct vnic_rq * rq,unsigned int count)141a6a5580cSJeff Kirsher static inline void vnic_rq_return_descs(struct vnic_rq *rq, unsigned int count)
142a6a5580cSJeff Kirsher {
143a6a5580cSJeff Kirsher 	rq->ring.desc_avail += count;
144a6a5580cSJeff Kirsher }
145a6a5580cSJeff Kirsher 
146a6a5580cSJeff Kirsher enum desc_return_options {
147a6a5580cSJeff Kirsher 	VNIC_RQ_RETURN_DESC,
148a6a5580cSJeff Kirsher 	VNIC_RQ_DEFER_RETURN_DESC,
149a6a5580cSJeff Kirsher };
150a6a5580cSJeff Kirsher 
vnic_rq_service(struct vnic_rq * rq,struct cq_desc * cq_desc,u16 completed_index,int desc_return,void (* buf_service)(struct vnic_rq * rq,struct cq_desc * cq_desc,struct vnic_rq_buf * buf,int skipped,void * opaque),void * opaque)151a6a5580cSJeff Kirsher static inline void vnic_rq_service(struct vnic_rq *rq,
152a6a5580cSJeff Kirsher 	struct cq_desc *cq_desc, u16 completed_index,
153a6a5580cSJeff Kirsher 	int desc_return, void (*buf_service)(struct vnic_rq *rq,
154a6a5580cSJeff Kirsher 	struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
155a6a5580cSJeff Kirsher 	int skipped, void *opaque), void *opaque)
156a6a5580cSJeff Kirsher {
157a6a5580cSJeff Kirsher 	struct vnic_rq_buf *buf;
158a6a5580cSJeff Kirsher 	int skipped;
159a6a5580cSJeff Kirsher 
160a6a5580cSJeff Kirsher 	buf = rq->to_clean;
161a6a5580cSJeff Kirsher 	while (1) {
162a6a5580cSJeff Kirsher 
163a6a5580cSJeff Kirsher 		skipped = (buf->index != completed_index);
164a6a5580cSJeff Kirsher 
165a6a5580cSJeff Kirsher 		(*buf_service)(rq, cq_desc, buf, skipped, opaque);
166a6a5580cSJeff Kirsher 
167a6a5580cSJeff Kirsher 		if (desc_return == VNIC_RQ_RETURN_DESC)
168a6a5580cSJeff Kirsher 			rq->ring.desc_avail++;
169a6a5580cSJeff Kirsher 
170a6a5580cSJeff Kirsher 		rq->to_clean = buf->next;
171a6a5580cSJeff Kirsher 
172a6a5580cSJeff Kirsher 		if (!skipped)
173a6a5580cSJeff Kirsher 			break;
174a6a5580cSJeff Kirsher 
175a6a5580cSJeff Kirsher 		buf = rq->to_clean;
176a6a5580cSJeff Kirsher 	}
177a6a5580cSJeff Kirsher }
178a6a5580cSJeff Kirsher 
vnic_rq_fill(struct vnic_rq * rq,int (* buf_fill)(struct vnic_rq * rq))179a6a5580cSJeff Kirsher static inline int vnic_rq_fill(struct vnic_rq *rq,
180a6a5580cSJeff Kirsher 	int (*buf_fill)(struct vnic_rq *rq))
181a6a5580cSJeff Kirsher {
182a6a5580cSJeff Kirsher 	int err;
183a6a5580cSJeff Kirsher 
184a6a5580cSJeff Kirsher 	while (vnic_rq_desc_avail(rq) > 0) {
185a6a5580cSJeff Kirsher 
186a6a5580cSJeff Kirsher 		err = (*buf_fill)(rq);
187a6a5580cSJeff Kirsher 		if (err)
188a6a5580cSJeff Kirsher 			return err;
189a6a5580cSJeff Kirsher 	}
190a6a5580cSJeff Kirsher 
191a6a5580cSJeff Kirsher 	return 0;
192a6a5580cSJeff Kirsher }
193a6a5580cSJeff Kirsher 
194a6a5580cSJeff Kirsher void vnic_rq_free(struct vnic_rq *rq);
195a6a5580cSJeff Kirsher int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
196a6a5580cSJeff Kirsher 	unsigned int desc_count, unsigned int desc_size);
197a6a5580cSJeff Kirsher void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
198a6a5580cSJeff Kirsher 	unsigned int error_interrupt_enable,
199a6a5580cSJeff Kirsher 	unsigned int error_interrupt_offset);
200a6a5580cSJeff Kirsher unsigned int vnic_rq_error_status(struct vnic_rq *rq);
201a6a5580cSJeff Kirsher void vnic_rq_enable(struct vnic_rq *rq);
202a6a5580cSJeff Kirsher int vnic_rq_disable(struct vnic_rq *rq);
203a6a5580cSJeff Kirsher void vnic_rq_clean(struct vnic_rq *rq,
204a6a5580cSJeff Kirsher 	void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf));
205a6a5580cSJeff Kirsher 
206a6a5580cSJeff Kirsher #endif /* _VNIC_RQ_H_ */
207