1/*
2 * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18
19#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include <linux/slab.h>
24#include "vnic_dev.h"
25#include "vnic_rq.h"
26
27static int vnic_rq_alloc_bufs(struct vnic_rq *rq)
28{
29	struct vnic_rq_buf *buf;
30	struct vnic_dev *vdev;
31	unsigned int i, j, count = rq->ring.desc_count;
32	unsigned int blks = VNIC_RQ_BUF_BLKS_NEEDED(count);
33
34	vdev = rq->vdev;
35
36	for (i = 0; i < blks; i++) {
37		rq->bufs[i] = kzalloc(VNIC_RQ_BUF_BLK_SZ, GFP_ATOMIC);
38		if (!rq->bufs[i]) {
39			printk(KERN_ERR "Failed to alloc rq_bufs\n");
40			return -ENOMEM;
41		}
42	}
43
44	for (i = 0; i < blks; i++) {
45		buf = rq->bufs[i];
46		for (j = 0; j < VNIC_RQ_BUF_BLK_ENTRIES; j++) {
47			buf->index = i * VNIC_RQ_BUF_BLK_ENTRIES + j;
48			buf->desc = (u8 *)rq->ring.descs +
49				rq->ring.desc_size * buf->index;
50			if (buf->index + 1 == count) {
51				buf->next = rq->bufs[0];
52				break;
53			} else if (j + 1 == VNIC_RQ_BUF_BLK_ENTRIES) {
54				buf->next = rq->bufs[i + 1];
55			} else {
56				buf->next = buf + 1;
57				buf++;
58			}
59		}
60	}
61
62	rq->to_use = rq->to_clean = rq->bufs[0];
63	rq->buf_index = 0;
64
65	return 0;
66}
67
68void vnic_rq_free(struct vnic_rq *rq)
69{
70	struct vnic_dev *vdev;
71	unsigned int i;
72
73	vdev = rq->vdev;
74
75	vnic_dev_free_desc_ring(vdev, &rq->ring);
76
77	for (i = 0; i < VNIC_RQ_BUF_BLKS_MAX; i++) {
78		kfree(rq->bufs[i]);
79		rq->bufs[i] = NULL;
80	}
81
82	rq->ctrl = NULL;
83}
84
85int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
86	unsigned int desc_count, unsigned int desc_size)
87{
88	int err;
89
90	rq->index = index;
91	rq->vdev = vdev;
92
93	rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index);
94	if (!rq->ctrl) {
95		printk(KERN_ERR "Failed to hook RQ[%d] resource\n", index);
96		return -EINVAL;
97	}
98
99	vnic_rq_disable(rq);
100
101	err = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size);
102	if (err)
103		return err;
104
105	err = vnic_rq_alloc_bufs(rq);
106	if (err) {
107		vnic_rq_free(rq);
108		return err;
109	}
110
111	return 0;
112}
113
114void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
115	unsigned int error_interrupt_enable,
116	unsigned int error_interrupt_offset)
117{
118	u64 paddr;
119	u32 fetch_index;
120
121	paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET;
122	writeq(paddr, &rq->ctrl->ring_base);
123	iowrite32(rq->ring.desc_count, &rq->ctrl->ring_size);
124	iowrite32(cq_index, &rq->ctrl->cq_index);
125	iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable);
126	iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset);
127	iowrite32(0, &rq->ctrl->dropped_packet_count);
128	iowrite32(0, &rq->ctrl->error_status);
129
130	/* Use current fetch_index as the ring starting point */
131	fetch_index = ioread32(&rq->ctrl->fetch_index);
132	rq->to_use = rq->to_clean =
133		&rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
134			[fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
135	iowrite32(fetch_index, &rq->ctrl->posted_index);
136
137	rq->buf_index = 0;
138}
139
140unsigned int vnic_rq_error_status(struct vnic_rq *rq)
141{
142	return ioread32(&rq->ctrl->error_status);
143}
144
145void vnic_rq_enable(struct vnic_rq *rq)
146{
147	iowrite32(1, &rq->ctrl->enable);
148}
149
150int vnic_rq_disable(struct vnic_rq *rq)
151{
152	unsigned int wait;
153
154	iowrite32(0, &rq->ctrl->enable);
155
156	/* Wait for HW to ACK disable request */
157	for (wait = 0; wait < 100; wait++) {
158		if (!(ioread32(&rq->ctrl->running)))
159			return 0;
160		udelay(1);
161	}
162
163	printk(KERN_ERR "Failed to disable RQ[%d]\n", rq->index);
164
165	return -ETIMEDOUT;
166}
167
168void vnic_rq_clean(struct vnic_rq *rq,
169	void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf))
170{
171	struct vnic_rq_buf *buf;
172	u32 fetch_index;
173
174	BUG_ON(ioread32(&rq->ctrl->enable));
175
176	buf = rq->to_clean;
177
178	while (vnic_rq_desc_used(rq) > 0) {
179
180		(*buf_clean)(rq, buf);
181
182		buf = rq->to_clean = buf->next;
183		rq->ring.desc_avail++;
184	}
185
186	/* Use current fetch_index as the ring starting point */
187	fetch_index = ioread32(&rq->ctrl->fetch_index);
188	rq->to_use = rq->to_clean =
189		&rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
190			[fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
191	iowrite32(fetch_index, &rq->ctrl->posted_index);
192
193	rq->buf_index = 0;
194
195	vnic_dev_clear_desc_ring(&rq->ring);
196}
197
198