xref: /linux/drivers/net/ethernet/cisco/enic/vnic_rq.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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_rq.h"
166a3c2f83SGovindarajulu Varadarajan #include "enic.h"
17a6a5580cSJeff Kirsher 
vnic_rq_alloc_bufs(struct vnic_rq * rq)18a6a5580cSJeff Kirsher static int vnic_rq_alloc_bufs(struct vnic_rq *rq)
19a6a5580cSJeff Kirsher {
20a6a5580cSJeff Kirsher 	struct vnic_rq_buf *buf;
21a6a5580cSJeff Kirsher 	unsigned int i, j, count = rq->ring.desc_count;
22a6a5580cSJeff Kirsher 	unsigned int blks = VNIC_RQ_BUF_BLKS_NEEDED(count);
23a6a5580cSJeff Kirsher 
24a6a5580cSJeff Kirsher 	for (i = 0; i < blks; i++) {
25039b1d5eSJia-Ju Bai 		rq->bufs[i] = kzalloc(VNIC_RQ_BUF_BLK_SZ(count), GFP_KERNEL);
26e404decbSJoe Perches 		if (!rq->bufs[i])
27a6a5580cSJeff Kirsher 			return -ENOMEM;
28a6a5580cSJeff Kirsher 	}
29a6a5580cSJeff Kirsher 
30a6a5580cSJeff Kirsher 	for (i = 0; i < blks; i++) {
31a6a5580cSJeff Kirsher 		buf = rq->bufs[i];
32a6a5580cSJeff Kirsher 		for (j = 0; j < VNIC_RQ_BUF_BLK_ENTRIES(count); j++) {
33a6a5580cSJeff Kirsher 			buf->index = i * VNIC_RQ_BUF_BLK_ENTRIES(count) + j;
34a6a5580cSJeff Kirsher 			buf->desc = (u8 *)rq->ring.descs +
35a6a5580cSJeff Kirsher 				rq->ring.desc_size * buf->index;
36a6a5580cSJeff Kirsher 			if (buf->index + 1 == count) {
37a6a5580cSJeff Kirsher 				buf->next = rq->bufs[0];
38a6a5580cSJeff Kirsher 				break;
39a6a5580cSJeff Kirsher 			} else if (j + 1 == VNIC_RQ_BUF_BLK_ENTRIES(count)) {
40a6a5580cSJeff Kirsher 				buf->next = rq->bufs[i + 1];
41a6a5580cSJeff Kirsher 			} else {
42a6a5580cSJeff Kirsher 				buf->next = buf + 1;
43a6a5580cSJeff Kirsher 				buf++;
44a6a5580cSJeff Kirsher 			}
45a6a5580cSJeff Kirsher 		}
46a6a5580cSJeff Kirsher 	}
47a6a5580cSJeff Kirsher 
48a6a5580cSJeff Kirsher 	rq->to_use = rq->to_clean = rq->bufs[0];
49a6a5580cSJeff Kirsher 
50a6a5580cSJeff Kirsher 	return 0;
51a6a5580cSJeff Kirsher }
52a6a5580cSJeff Kirsher 
vnic_rq_free(struct vnic_rq * rq)53a6a5580cSJeff Kirsher void vnic_rq_free(struct vnic_rq *rq)
54a6a5580cSJeff Kirsher {
55a6a5580cSJeff Kirsher 	struct vnic_dev *vdev;
56a6a5580cSJeff Kirsher 	unsigned int i;
57a6a5580cSJeff Kirsher 
58a6a5580cSJeff Kirsher 	vdev = rq->vdev;
59a6a5580cSJeff Kirsher 
60a6a5580cSJeff Kirsher 	vnic_dev_free_desc_ring(vdev, &rq->ring);
61a6a5580cSJeff Kirsher 
62a6a5580cSJeff Kirsher 	for (i = 0; i < VNIC_RQ_BUF_BLKS_MAX; i++) {
63a6a5580cSJeff Kirsher 		if (rq->bufs[i]) {
64a6a5580cSJeff Kirsher 			kfree(rq->bufs[i]);
65a6a5580cSJeff Kirsher 			rq->bufs[i] = NULL;
66a6a5580cSJeff Kirsher 		}
67a6a5580cSJeff Kirsher 	}
68a6a5580cSJeff Kirsher 
69a6a5580cSJeff Kirsher 	rq->ctrl = NULL;
70a6a5580cSJeff Kirsher }
71a6a5580cSJeff Kirsher 
vnic_rq_alloc(struct vnic_dev * vdev,struct vnic_rq * rq,unsigned int index,unsigned int desc_count,unsigned int desc_size)72a6a5580cSJeff Kirsher int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
73a6a5580cSJeff Kirsher 	unsigned int desc_count, unsigned int desc_size)
74a6a5580cSJeff Kirsher {
75a6a5580cSJeff Kirsher 	int err;
76a6a5580cSJeff Kirsher 
77a6a5580cSJeff Kirsher 	rq->index = index;
78a6a5580cSJeff Kirsher 	rq->vdev = vdev;
79a6a5580cSJeff Kirsher 
80a6a5580cSJeff Kirsher 	rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index);
81a6a5580cSJeff Kirsher 	if (!rq->ctrl) {
82e327f4e1SJoe Perches 		vdev_err(vdev, "Failed to hook RQ[%d] resource\n", index);
83a6a5580cSJeff Kirsher 		return -EINVAL;
84a6a5580cSJeff Kirsher 	}
85a6a5580cSJeff Kirsher 
86a6a5580cSJeff Kirsher 	vnic_rq_disable(rq);
87a6a5580cSJeff Kirsher 
88a6a5580cSJeff Kirsher 	err = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size);
89a6a5580cSJeff Kirsher 	if (err)
90a6a5580cSJeff Kirsher 		return err;
91a6a5580cSJeff Kirsher 
92a6a5580cSJeff Kirsher 	err = vnic_rq_alloc_bufs(rq);
93a6a5580cSJeff Kirsher 	if (err) {
94a6a5580cSJeff Kirsher 		vnic_rq_free(rq);
95a6a5580cSJeff Kirsher 		return err;
96a6a5580cSJeff Kirsher 	}
97a6a5580cSJeff Kirsher 
98a6a5580cSJeff Kirsher 	return 0;
99a6a5580cSJeff Kirsher }
100a6a5580cSJeff Kirsher 
vnic_rq_init_start(struct vnic_rq * rq,unsigned int cq_index,unsigned int fetch_index,unsigned int posted_index,unsigned int error_interrupt_enable,unsigned int error_interrupt_offset)101a6a5580cSJeff Kirsher static void vnic_rq_init_start(struct vnic_rq *rq, unsigned int cq_index,
102a6a5580cSJeff Kirsher 	unsigned int fetch_index, unsigned int posted_index,
103a6a5580cSJeff Kirsher 	unsigned int error_interrupt_enable,
104a6a5580cSJeff Kirsher 	unsigned int error_interrupt_offset)
105a6a5580cSJeff Kirsher {
106a6a5580cSJeff Kirsher 	u64 paddr;
107a6a5580cSJeff Kirsher 	unsigned int count = rq->ring.desc_count;
108a6a5580cSJeff Kirsher 
109a6a5580cSJeff Kirsher 	paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET;
110a6a5580cSJeff Kirsher 	writeq(paddr, &rq->ctrl->ring_base);
111a6a5580cSJeff Kirsher 	iowrite32(count, &rq->ctrl->ring_size);
112a6a5580cSJeff Kirsher 	iowrite32(cq_index, &rq->ctrl->cq_index);
113a6a5580cSJeff Kirsher 	iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable);
114a6a5580cSJeff Kirsher 	iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset);
115a6a5580cSJeff Kirsher 	iowrite32(0, &rq->ctrl->dropped_packet_count);
116a6a5580cSJeff Kirsher 	iowrite32(0, &rq->ctrl->error_status);
117a6a5580cSJeff Kirsher 	iowrite32(fetch_index, &rq->ctrl->fetch_index);
118a6a5580cSJeff Kirsher 	iowrite32(posted_index, &rq->ctrl->posted_index);
119a6a5580cSJeff Kirsher 
120a6a5580cSJeff Kirsher 	rq->to_use = rq->to_clean =
121a6a5580cSJeff Kirsher 		&rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES(count)]
122a6a5580cSJeff Kirsher 			[fetch_index % VNIC_RQ_BUF_BLK_ENTRIES(count)];
123a6a5580cSJeff Kirsher }
124a6a5580cSJeff Kirsher 
vnic_rq_init(struct vnic_rq * rq,unsigned int cq_index,unsigned int error_interrupt_enable,unsigned int error_interrupt_offset)125a6a5580cSJeff Kirsher void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
126a6a5580cSJeff Kirsher 	unsigned int error_interrupt_enable,
127a6a5580cSJeff Kirsher 	unsigned int error_interrupt_offset)
128a6a5580cSJeff Kirsher {
129e6cdfcc5SParvi Kaustubhi 	vnic_rq_init_start(rq, cq_index, 0, 0, error_interrupt_enable,
130a6a5580cSJeff Kirsher 			   error_interrupt_offset);
131a6a5580cSJeff Kirsher }
132a6a5580cSJeff Kirsher 
vnic_rq_error_status(struct vnic_rq * rq)133a6a5580cSJeff Kirsher unsigned int vnic_rq_error_status(struct vnic_rq *rq)
134a6a5580cSJeff Kirsher {
135a6a5580cSJeff Kirsher 	return ioread32(&rq->ctrl->error_status);
136a6a5580cSJeff Kirsher }
137a6a5580cSJeff Kirsher 
vnic_rq_enable(struct vnic_rq * rq)138a6a5580cSJeff Kirsher void vnic_rq_enable(struct vnic_rq *rq)
139a6a5580cSJeff Kirsher {
140a6a5580cSJeff Kirsher 	iowrite32(1, &rq->ctrl->enable);
141a6a5580cSJeff Kirsher }
142a6a5580cSJeff Kirsher 
vnic_rq_disable(struct vnic_rq * rq)143a6a5580cSJeff Kirsher int vnic_rq_disable(struct vnic_rq *rq)
144a6a5580cSJeff Kirsher {
145a6a5580cSJeff Kirsher 	unsigned int wait;
1466a3c2f83SGovindarajulu Varadarajan 	struct vnic_dev *vdev = rq->vdev;
1479fe1c98aSGovindarajulu Varadarajan 	int i;
148a6a5580cSJeff Kirsher 
1499fe1c98aSGovindarajulu Varadarajan 	/* Due to a race condition with clearing RQ "mini-cache" in hw, we need
1509fe1c98aSGovindarajulu Varadarajan 	 * to disable the RQ twice to guarantee that stale descriptors are not
1519fe1c98aSGovindarajulu Varadarajan 	 * used when this RQ is re-enabled.
1529fe1c98aSGovindarajulu Varadarajan 	 */
1539fe1c98aSGovindarajulu Varadarajan 	for (i = 0; i < 2; i++) {
154a6a5580cSJeff Kirsher 		iowrite32(0, &rq->ctrl->enable);
155a6a5580cSJeff Kirsher 
156a6a5580cSJeff Kirsher 		/* Wait for HW to ACK disable request */
1579fe1c98aSGovindarajulu Varadarajan 		for (wait = 20000; wait > 0; wait--)
1589fe1c98aSGovindarajulu Varadarajan 			if (!ioread32(&rq->ctrl->running))
1599fe1c98aSGovindarajulu Varadarajan 				break;
1609fe1c98aSGovindarajulu Varadarajan 		if (!wait) {
1619fe1c98aSGovindarajulu Varadarajan 			vdev_neterr(vdev, "Failed to disable RQ[%d]\n",
1629fe1c98aSGovindarajulu Varadarajan 				    rq->index);
163a6a5580cSJeff Kirsher 
164a6a5580cSJeff Kirsher 			return -ETIMEDOUT;
165a6a5580cSJeff Kirsher 		}
1669fe1c98aSGovindarajulu Varadarajan 	}
1679fe1c98aSGovindarajulu Varadarajan 
1689fe1c98aSGovindarajulu Varadarajan 	return 0;
1699fe1c98aSGovindarajulu Varadarajan }
170a6a5580cSJeff Kirsher 
vnic_rq_clean(struct vnic_rq * rq,void (* buf_clean)(struct vnic_rq * rq,struct vnic_rq_buf * buf))171a6a5580cSJeff Kirsher void vnic_rq_clean(struct vnic_rq *rq,
172a6a5580cSJeff Kirsher 	void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf))
173a6a5580cSJeff Kirsher {
174a6a5580cSJeff Kirsher 	struct vnic_rq_buf *buf;
175a6a5580cSJeff Kirsher 	u32 fetch_index;
176a6a5580cSJeff Kirsher 	unsigned int count = rq->ring.desc_count;
1778b13b4e0SGovindarajulu Varadarajan 	int i;
178a6a5580cSJeff Kirsher 
179a6a5580cSJeff Kirsher 	buf = rq->to_clean;
180a6a5580cSJeff Kirsher 
1818b13b4e0SGovindarajulu Varadarajan 	for (i = 0; i < rq->ring.desc_count; i++) {
182a6a5580cSJeff Kirsher 		(*buf_clean)(rq, buf);
1838b13b4e0SGovindarajulu Varadarajan 		buf = buf->next;
184a6a5580cSJeff Kirsher 	}
1858b13b4e0SGovindarajulu Varadarajan 	rq->ring.desc_avail = rq->ring.desc_count - 1;
186a6a5580cSJeff Kirsher 
187a6a5580cSJeff Kirsher 	/* Use current fetch_index as the ring starting point */
188a6a5580cSJeff Kirsher 	fetch_index = ioread32(&rq->ctrl->fetch_index);
189a6a5580cSJeff Kirsher 
190a6a5580cSJeff Kirsher 	if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone  */
191a6a5580cSJeff Kirsher 		/* Hardware surprise removal: reset fetch_index */
192a6a5580cSJeff Kirsher 		fetch_index = 0;
193a6a5580cSJeff Kirsher 	}
194a6a5580cSJeff Kirsher 	rq->to_use = rq->to_clean =
195a6a5580cSJeff Kirsher 		&rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES(count)]
196a6a5580cSJeff Kirsher 			[fetch_index % VNIC_RQ_BUF_BLK_ENTRIES(count)];
197a6a5580cSJeff Kirsher 	iowrite32(fetch_index, &rq->ctrl->posted_index);
198a6a5580cSJeff Kirsher 
1999fe1c98aSGovindarajulu Varadarajan 	/* Anytime we write fetch_index, we need to re-write 0 to rq->enable
2009fe1c98aSGovindarajulu Varadarajan 	 * to re-sync internal VIC state.
2019fe1c98aSGovindarajulu Varadarajan 	 */
2029fe1c98aSGovindarajulu Varadarajan 	iowrite32(0, &rq->ctrl->enable);
2039fe1c98aSGovindarajulu Varadarajan 
204a6a5580cSJeff Kirsher 	vnic_dev_clear_desc_ring(&rq->ring);
205a6a5580cSJeff Kirsher }
206