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 #include <linux/kernel.h>
8a6a5580cSJeff Kirsher #include <linux/errno.h>
9a6a5580cSJeff Kirsher #include <linux/types.h>
10a6a5580cSJeff Kirsher #include <linux/pci.h>
11a6a5580cSJeff Kirsher #include <linux/delay.h>
12a6a5580cSJeff Kirsher #include <linux/slab.h>
13a6a5580cSJeff Kirsher 
14a6a5580cSJeff Kirsher #include "vnic_dev.h"
15a6a5580cSJeff Kirsher #include "vnic_wq.h"
166a3c2f83SGovindarajulu Varadarajan #include "enic.h"
17a6a5580cSJeff Kirsher 
vnic_wq_alloc_bufs(struct vnic_wq * wq)18a6a5580cSJeff Kirsher static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
19a6a5580cSJeff Kirsher {
20a6a5580cSJeff Kirsher 	struct vnic_wq_buf *buf;
21a6a5580cSJeff Kirsher 	unsigned int i, j, count = wq->ring.desc_count;
22a6a5580cSJeff Kirsher 	unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
23a6a5580cSJeff Kirsher 
24a6a5580cSJeff Kirsher 	for (i = 0; i < blks; i++) {
25039b1d5eSJia-Ju Bai 		wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ(count), GFP_KERNEL);
26e404decbSJoe Perches 		if (!wq->bufs[i])
27a6a5580cSJeff Kirsher 			return -ENOMEM;
28a6a5580cSJeff Kirsher 	}
29a6a5580cSJeff Kirsher 
30a6a5580cSJeff Kirsher 	for (i = 0; i < blks; i++) {
31a6a5580cSJeff Kirsher 		buf = wq->bufs[i];
32a6a5580cSJeff Kirsher 		for (j = 0; j < VNIC_WQ_BUF_BLK_ENTRIES(count); j++) {
33a6a5580cSJeff Kirsher 			buf->index = i * VNIC_WQ_BUF_BLK_ENTRIES(count) + j;
34a6a5580cSJeff Kirsher 			buf->desc = (u8 *)wq->ring.descs +
35a6a5580cSJeff Kirsher 				wq->ring.desc_size * buf->index;
36a6a5580cSJeff Kirsher 			if (buf->index + 1 == count) {
37a6a5580cSJeff Kirsher 				buf->next = wq->bufs[0];
385e32066dSGovindarajulu Varadarajan 				buf->next->prev = buf;
39a6a5580cSJeff Kirsher 				break;
40a6a5580cSJeff Kirsher 			} else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES(count)) {
41a6a5580cSJeff Kirsher 				buf->next = wq->bufs[i + 1];
425e32066dSGovindarajulu Varadarajan 				buf->next->prev = buf;
43a6a5580cSJeff Kirsher 			} else {
44a6a5580cSJeff Kirsher 				buf->next = buf + 1;
455e32066dSGovindarajulu Varadarajan 				buf->next->prev = buf;
46a6a5580cSJeff Kirsher 				buf++;
47a6a5580cSJeff Kirsher 			}
48a6a5580cSJeff Kirsher 		}
49a6a5580cSJeff Kirsher 	}
50a6a5580cSJeff Kirsher 
51a6a5580cSJeff Kirsher 	wq->to_use = wq->to_clean = wq->bufs[0];
52a6a5580cSJeff Kirsher 
53a6a5580cSJeff Kirsher 	return 0;
54a6a5580cSJeff Kirsher }
55a6a5580cSJeff Kirsher 
vnic_wq_free(struct vnic_wq * wq)56a6a5580cSJeff Kirsher void vnic_wq_free(struct vnic_wq *wq)
57a6a5580cSJeff Kirsher {
58a6a5580cSJeff Kirsher 	struct vnic_dev *vdev;
59a6a5580cSJeff Kirsher 	unsigned int i;
60a6a5580cSJeff Kirsher 
61a6a5580cSJeff Kirsher 	vdev = wq->vdev;
62a6a5580cSJeff Kirsher 
63a6a5580cSJeff Kirsher 	vnic_dev_free_desc_ring(vdev, &wq->ring);
64a6a5580cSJeff Kirsher 
65a6a5580cSJeff Kirsher 	for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
66a6a5580cSJeff Kirsher 		if (wq->bufs[i]) {
67a6a5580cSJeff Kirsher 			kfree(wq->bufs[i]);
68a6a5580cSJeff Kirsher 			wq->bufs[i] = NULL;
69a6a5580cSJeff Kirsher 		}
70a6a5580cSJeff Kirsher 	}
71a6a5580cSJeff Kirsher 
72a6a5580cSJeff Kirsher 	wq->ctrl = NULL;
73a6a5580cSJeff Kirsher }
74a6a5580cSJeff Kirsher 
vnic_wq_alloc(struct vnic_dev * vdev,struct vnic_wq * wq,unsigned int index,unsigned int desc_count,unsigned int desc_size)75a6a5580cSJeff Kirsher int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
76a6a5580cSJeff Kirsher 	unsigned int desc_count, unsigned int desc_size)
77a6a5580cSJeff Kirsher {
78a6a5580cSJeff Kirsher 	int err;
79a6a5580cSJeff Kirsher 
80a6a5580cSJeff Kirsher 	wq->index = index;
81a6a5580cSJeff Kirsher 	wq->vdev = vdev;
82a6a5580cSJeff Kirsher 
83a6a5580cSJeff Kirsher 	wq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_WQ, index);
84a6a5580cSJeff Kirsher 	if (!wq->ctrl) {
85e327f4e1SJoe Perches 		vdev_err(vdev, "Failed to hook WQ[%d] resource\n", index);
86a6a5580cSJeff Kirsher 		return -EINVAL;
87a6a5580cSJeff Kirsher 	}
88a6a5580cSJeff Kirsher 
89a6a5580cSJeff Kirsher 	vnic_wq_disable(wq);
90a6a5580cSJeff Kirsher 
91a6a5580cSJeff Kirsher 	err = vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size);
92a6a5580cSJeff Kirsher 	if (err)
93a6a5580cSJeff Kirsher 		return err;
94a6a5580cSJeff Kirsher 
95a6a5580cSJeff Kirsher 	err = vnic_wq_alloc_bufs(wq);
96a6a5580cSJeff Kirsher 	if (err) {
97a6a5580cSJeff Kirsher 		vnic_wq_free(wq);
98a6a5580cSJeff Kirsher 		return err;
99a6a5580cSJeff Kirsher 	}
100a6a5580cSJeff Kirsher 
101a6a5580cSJeff Kirsher 	return 0;
102a6a5580cSJeff Kirsher }
103a6a5580cSJeff Kirsher 
enic_wq_devcmd2_alloc(struct vnic_dev * vdev,struct vnic_wq * wq,unsigned int desc_count,unsigned int desc_size)1043dc33e23SDavid S. Miller int enic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
105373fb087SGovindarajulu Varadarajan 			  unsigned int desc_count, unsigned int desc_size)
106373fb087SGovindarajulu Varadarajan {
107373fb087SGovindarajulu Varadarajan 	int err;
108373fb087SGovindarajulu Varadarajan 
109373fb087SGovindarajulu Varadarajan 	wq->index = 0;
110373fb087SGovindarajulu Varadarajan 	wq->vdev = vdev;
111373fb087SGovindarajulu Varadarajan 
112373fb087SGovindarajulu Varadarajan 	wq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
113373fb087SGovindarajulu Varadarajan 	if (!wq->ctrl)
114373fb087SGovindarajulu Varadarajan 		return -EINVAL;
115373fb087SGovindarajulu Varadarajan 	vnic_wq_disable(wq);
116373fb087SGovindarajulu Varadarajan 	err = vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size);
117373fb087SGovindarajulu Varadarajan 
118373fb087SGovindarajulu Varadarajan 	return err;
119373fb087SGovindarajulu Varadarajan }
120373fb087SGovindarajulu Varadarajan 
enic_wq_init_start(struct vnic_wq * wq,unsigned int cq_index,unsigned int fetch_index,unsigned int posted_index,unsigned int error_interrupt_enable,unsigned int error_interrupt_offset)1213dc33e23SDavid S. Miller void enic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
122a6a5580cSJeff Kirsher 			unsigned int fetch_index, unsigned int posted_index,
123a6a5580cSJeff Kirsher 			unsigned int error_interrupt_enable,
124a6a5580cSJeff Kirsher 			unsigned int error_interrupt_offset)
125a6a5580cSJeff Kirsher {
126a6a5580cSJeff Kirsher 	u64 paddr;
127a6a5580cSJeff Kirsher 	unsigned int count = wq->ring.desc_count;
128a6a5580cSJeff Kirsher 
129a6a5580cSJeff Kirsher 	paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
130a6a5580cSJeff Kirsher 	writeq(paddr, &wq->ctrl->ring_base);
131a6a5580cSJeff Kirsher 	iowrite32(count, &wq->ctrl->ring_size);
132a6a5580cSJeff Kirsher 	iowrite32(fetch_index, &wq->ctrl->fetch_index);
133a6a5580cSJeff Kirsher 	iowrite32(posted_index, &wq->ctrl->posted_index);
134a6a5580cSJeff Kirsher 	iowrite32(cq_index, &wq->ctrl->cq_index);
135a6a5580cSJeff Kirsher 	iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
136a6a5580cSJeff Kirsher 	iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
137a6a5580cSJeff Kirsher 	iowrite32(0, &wq->ctrl->error_status);
138a6a5580cSJeff Kirsher 
139a6a5580cSJeff Kirsher 	wq->to_use = wq->to_clean =
140a6a5580cSJeff Kirsher 		&wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)]
141a6a5580cSJeff Kirsher 			[fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)];
142a6a5580cSJeff Kirsher }
143a6a5580cSJeff Kirsher 
vnic_wq_init(struct vnic_wq * wq,unsigned int cq_index,unsigned int error_interrupt_enable,unsigned int error_interrupt_offset)144a6a5580cSJeff Kirsher void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
145a6a5580cSJeff Kirsher 	unsigned int error_interrupt_enable,
146a6a5580cSJeff Kirsher 	unsigned int error_interrupt_offset)
147a6a5580cSJeff Kirsher {
1483dc33e23SDavid S. Miller 	enic_wq_init_start(wq, cq_index, 0, 0,
149a6a5580cSJeff Kirsher 		error_interrupt_enable,
150a6a5580cSJeff Kirsher 		error_interrupt_offset);
151a6a5580cSJeff Kirsher }
152a6a5580cSJeff Kirsher 
vnic_wq_error_status(struct vnic_wq * wq)153a6a5580cSJeff Kirsher unsigned int vnic_wq_error_status(struct vnic_wq *wq)
154a6a5580cSJeff Kirsher {
155a6a5580cSJeff Kirsher 	return ioread32(&wq->ctrl->error_status);
156a6a5580cSJeff Kirsher }
157a6a5580cSJeff Kirsher 
vnic_wq_enable(struct vnic_wq * wq)158a6a5580cSJeff Kirsher void vnic_wq_enable(struct vnic_wq *wq)
159a6a5580cSJeff Kirsher {
160a6a5580cSJeff Kirsher 	iowrite32(1, &wq->ctrl->enable);
161a6a5580cSJeff Kirsher }
162a6a5580cSJeff Kirsher 
vnic_wq_disable(struct vnic_wq * wq)163a6a5580cSJeff Kirsher int vnic_wq_disable(struct vnic_wq *wq)
164a6a5580cSJeff Kirsher {
165a6a5580cSJeff Kirsher 	unsigned int wait;
1666a3c2f83SGovindarajulu Varadarajan 	struct vnic_dev *vdev = wq->vdev;
167a6a5580cSJeff Kirsher 
168a6a5580cSJeff Kirsher 	iowrite32(0, &wq->ctrl->enable);
169a6a5580cSJeff Kirsher 
170a6a5580cSJeff Kirsher 	/* Wait for HW to ACK disable request */
171a6a5580cSJeff Kirsher 	for (wait = 0; wait < 1000; wait++) {
172a6a5580cSJeff Kirsher 		if (!(ioread32(&wq->ctrl->running)))
173a6a5580cSJeff Kirsher 			return 0;
174a6a5580cSJeff Kirsher 		udelay(10);
175a6a5580cSJeff Kirsher 	}
176a6a5580cSJeff Kirsher 
177e327f4e1SJoe Perches 	vdev_neterr(vdev, "Failed to disable WQ[%d]\n", wq->index);
178a6a5580cSJeff Kirsher 
179a6a5580cSJeff Kirsher 	return -ETIMEDOUT;
180a6a5580cSJeff Kirsher }
181a6a5580cSJeff Kirsher 
vnic_wq_clean(struct vnic_wq * wq,void (* buf_clean)(struct vnic_wq * wq,struct vnic_wq_buf * buf))182a6a5580cSJeff Kirsher void vnic_wq_clean(struct vnic_wq *wq,
183a6a5580cSJeff Kirsher 	void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
184a6a5580cSJeff Kirsher {
185a6a5580cSJeff Kirsher 	struct vnic_wq_buf *buf;
186a6a5580cSJeff Kirsher 
187a6a5580cSJeff Kirsher 	buf = wq->to_clean;
188a6a5580cSJeff Kirsher 
189a6a5580cSJeff Kirsher 	while (vnic_wq_desc_used(wq) > 0) {
190a6a5580cSJeff Kirsher 
191a6a5580cSJeff Kirsher 		(*buf_clean)(wq, buf);
192a6a5580cSJeff Kirsher 
193a6a5580cSJeff Kirsher 		buf = wq->to_clean = buf->next;
194a6a5580cSJeff Kirsher 		wq->ring.desc_avail++;
195a6a5580cSJeff Kirsher 	}
196a6a5580cSJeff Kirsher 
197a6a5580cSJeff Kirsher 	wq->to_use = wq->to_clean = wq->bufs[0];
198a6a5580cSJeff Kirsher 
199a6a5580cSJeff Kirsher 	iowrite32(0, &wq->ctrl->fetch_index);
200a6a5580cSJeff Kirsher 	iowrite32(0, &wq->ctrl->posted_index);
201a6a5580cSJeff Kirsher 	iowrite32(0, &wq->ctrl->error_status);
202a6a5580cSJeff Kirsher 
203a6a5580cSJeff Kirsher 	vnic_dev_clear_desc_ring(&wq->ring);
204a6a5580cSJeff Kirsher }
205