18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This program is free software; you may redistribute it and/or modify
68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by
78c2ecf20Sopenharmony_ci * the Free Software Foundation; version 2 of the License.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
108c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
128c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
138c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
148c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
158c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
168c2ecf20Sopenharmony_ci * SOFTWARE.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/errno.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/pci.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include "vnic_dev.h"
258c2ecf20Sopenharmony_ci#include "vnic_wq.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
298c2ecf20Sopenharmony_ci		unsigned int index, enum vnic_res_type res_type)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	wq->ctrl = vnic_dev_get_res(vdev, res_type, index);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (!wq->ctrl)
348c2ecf20Sopenharmony_ci		return -EINVAL;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	return 0;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
418c2ecf20Sopenharmony_ci		unsigned int desc_count, unsigned int desc_size)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	return vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int vnic_wq_alloc_bufs(struct vnic_wq *wq)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct vnic_wq_buf *buf;
508c2ecf20Sopenharmony_ci	unsigned int i, j, count = wq->ring.desc_count;
518c2ecf20Sopenharmony_ci	unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	for (i = 0; i < blks; i++) {
548c2ecf20Sopenharmony_ci		wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ, GFP_ATOMIC);
558c2ecf20Sopenharmony_ci		if (!wq->bufs[i]) {
568c2ecf20Sopenharmony_ci			printk(KERN_ERR "Failed to alloc wq_bufs\n");
578c2ecf20Sopenharmony_ci			return -ENOMEM;
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	for (i = 0; i < blks; i++) {
628c2ecf20Sopenharmony_ci		buf = wq->bufs[i];
638c2ecf20Sopenharmony_ci		for (j = 0; j < VNIC_WQ_BUF_BLK_ENTRIES; j++) {
648c2ecf20Sopenharmony_ci			buf->index = i * VNIC_WQ_BUF_BLK_ENTRIES + j;
658c2ecf20Sopenharmony_ci			buf->desc = (u8 *)wq->ring.descs +
668c2ecf20Sopenharmony_ci				wq->ring.desc_size * buf->index;
678c2ecf20Sopenharmony_ci			if (buf->index + 1 == count) {
688c2ecf20Sopenharmony_ci				buf->next = wq->bufs[0];
698c2ecf20Sopenharmony_ci				break;
708c2ecf20Sopenharmony_ci			} else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES) {
718c2ecf20Sopenharmony_ci				buf->next = wq->bufs[i + 1];
728c2ecf20Sopenharmony_ci			} else {
738c2ecf20Sopenharmony_ci				buf->next = buf + 1;
748c2ecf20Sopenharmony_ci				buf++;
758c2ecf20Sopenharmony_ci			}
768c2ecf20Sopenharmony_ci		}
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	wq->to_use = wq->to_clean = wq->bufs[0];
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_civoid vnic_wq_free(struct vnic_wq *wq)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct vnic_dev *vdev;
878c2ecf20Sopenharmony_ci	unsigned int i;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	vdev = wq->vdev;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	vnic_dev_free_desc_ring(vdev, &wq->ring);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
948c2ecf20Sopenharmony_ci		kfree(wq->bufs[i]);
958c2ecf20Sopenharmony_ci		wq->bufs[i] = NULL;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	wq->ctrl = NULL;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciint vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
1038c2ecf20Sopenharmony_ci	unsigned int desc_count, unsigned int desc_size)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	int err;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	wq->index = index;
1088c2ecf20Sopenharmony_ci	wq->vdev = vdev;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	wq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_WQ, index);
1118c2ecf20Sopenharmony_ci	if (!wq->ctrl) {
1128c2ecf20Sopenharmony_ci		printk(KERN_ERR "Failed to hook WQ[%d] resource\n", index);
1138c2ecf20Sopenharmony_ci		return -EINVAL;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	vnic_wq_disable(wq);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	err = vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size);
1198c2ecf20Sopenharmony_ci	if (err)
1208c2ecf20Sopenharmony_ci		return err;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	err = vnic_wq_alloc_bufs(wq);
1238c2ecf20Sopenharmony_ci	if (err) {
1248c2ecf20Sopenharmony_ci		vnic_wq_free(wq);
1258c2ecf20Sopenharmony_ci		return err;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciint vnic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
1338c2ecf20Sopenharmony_ci		unsigned int desc_count, unsigned int desc_size)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	int err;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	wq->index = 0;
1388c2ecf20Sopenharmony_ci	wq->vdev = vdev;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	err = vnic_wq_get_ctrl(vdev, wq, 0, RES_TYPE_DEVCMD2);
1418c2ecf20Sopenharmony_ci	if (err) {
1428c2ecf20Sopenharmony_ci		pr_err("Failed to get devcmd2 resource\n");
1438c2ecf20Sopenharmony_ci		return err;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	vnic_wq_disable(wq);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
1488c2ecf20Sopenharmony_ci	if (err)
1498c2ecf20Sopenharmony_ci		return err;
1508c2ecf20Sopenharmony_ci	return 0;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_civoid vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
1548c2ecf20Sopenharmony_ci		unsigned int fetch_index, unsigned int posted_index,
1558c2ecf20Sopenharmony_ci		unsigned int error_interrupt_enable,
1568c2ecf20Sopenharmony_ci		unsigned int error_interrupt_offset)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	u64 paddr;
1598c2ecf20Sopenharmony_ci	unsigned int count = wq->ring.desc_count;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
1628c2ecf20Sopenharmony_ci	writeq(paddr, &wq->ctrl->ring_base);
1638c2ecf20Sopenharmony_ci	iowrite32(count, &wq->ctrl->ring_size);
1648c2ecf20Sopenharmony_ci	iowrite32(fetch_index, &wq->ctrl->fetch_index);
1658c2ecf20Sopenharmony_ci	iowrite32(posted_index, &wq->ctrl->posted_index);
1668c2ecf20Sopenharmony_ci	iowrite32(cq_index, &wq->ctrl->cq_index);
1678c2ecf20Sopenharmony_ci	iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
1688c2ecf20Sopenharmony_ci	iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
1698c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->error_status);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	wq->to_use = wq->to_clean =
1728c2ecf20Sopenharmony_ci		&wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES]
1738c2ecf20Sopenharmony_ci		[fetch_index % VNIC_WQ_BUF_BLK_ENTRIES];
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_civoid vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
1788c2ecf20Sopenharmony_ci	unsigned int error_interrupt_enable,
1798c2ecf20Sopenharmony_ci	unsigned int error_interrupt_offset)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	u64 paddr;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
1848c2ecf20Sopenharmony_ci	writeq(paddr, &wq->ctrl->ring_base);
1858c2ecf20Sopenharmony_ci	iowrite32(wq->ring.desc_count, &wq->ctrl->ring_size);
1868c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->fetch_index);
1878c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->posted_index);
1888c2ecf20Sopenharmony_ci	iowrite32(cq_index, &wq->ctrl->cq_index);
1898c2ecf20Sopenharmony_ci	iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
1908c2ecf20Sopenharmony_ci	iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
1918c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->error_status);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ciunsigned int vnic_wq_error_status(struct vnic_wq *wq)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	return ioread32(&wq->ctrl->error_status);
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_civoid vnic_wq_enable(struct vnic_wq *wq)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	iowrite32(1, &wq->ctrl->enable);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciint vnic_wq_disable(struct vnic_wq *wq)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	unsigned int wait;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->enable);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Wait for HW to ACK disable request */
2118c2ecf20Sopenharmony_ci	for (wait = 0; wait < 100; wait++) {
2128c2ecf20Sopenharmony_ci		if (!(ioread32(&wq->ctrl->running)))
2138c2ecf20Sopenharmony_ci			return 0;
2148c2ecf20Sopenharmony_ci		udelay(1);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	printk(KERN_ERR "Failed to disable WQ[%d]\n", wq->index);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_civoid vnic_wq_clean(struct vnic_wq *wq,
2238c2ecf20Sopenharmony_ci	void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct vnic_wq_buf *buf;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	BUG_ON(ioread32(&wq->ctrl->enable));
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	buf = wq->to_clean;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	while (vnic_wq_desc_used(wq) > 0) {
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		(*buf_clean)(wq, buf);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		buf = wq->to_clean = buf->next;
2368c2ecf20Sopenharmony_ci		wq->ring.desc_avail++;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	wq->to_use = wq->to_clean = wq->bufs[0];
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->fetch_index);
2428c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->posted_index);
2438c2ecf20Sopenharmony_ci	iowrite32(0, &wq->ctrl->error_status);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	vnic_dev_clear_desc_ring(&wq->ring);
2468c2ecf20Sopenharmony_ci}
247