1*e6550b3eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25df6d737SAbhijeet Joglekar /*
35df6d737SAbhijeet Joglekar * Copyright 2008 Cisco Systems, Inc. All rights reserved.
45df6d737SAbhijeet Joglekar * Copyright 2007 Nuova Systems, Inc. All rights reserved.
55df6d737SAbhijeet Joglekar */
65df6d737SAbhijeet Joglekar
75df6d737SAbhijeet Joglekar #include <linux/errno.h>
85df6d737SAbhijeet Joglekar #include <linux/types.h>
95df6d737SAbhijeet Joglekar #include <linux/pci.h>
105df6d737SAbhijeet Joglekar #include <linux/delay.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
125df6d737SAbhijeet Joglekar #include "vnic_dev.h"
135df6d737SAbhijeet Joglekar #include "vnic_rq.h"
145df6d737SAbhijeet Joglekar
vnic_rq_alloc_bufs(struct vnic_rq * rq)155df6d737SAbhijeet Joglekar static int vnic_rq_alloc_bufs(struct vnic_rq *rq)
165df6d737SAbhijeet Joglekar {
175df6d737SAbhijeet Joglekar struct vnic_rq_buf *buf;
185df6d737SAbhijeet Joglekar unsigned int i, j, count = rq->ring.desc_count;
195df6d737SAbhijeet Joglekar unsigned int blks = VNIC_RQ_BUF_BLKS_NEEDED(count);
205df6d737SAbhijeet Joglekar
215df6d737SAbhijeet Joglekar for (i = 0; i < blks; i++) {
225df6d737SAbhijeet Joglekar rq->bufs[i] = kzalloc(VNIC_RQ_BUF_BLK_SZ, GFP_ATOMIC);
235df6d737SAbhijeet Joglekar if (!rq->bufs[i]) {
245df6d737SAbhijeet Joglekar printk(KERN_ERR "Failed to alloc rq_bufs\n");
255df6d737SAbhijeet Joglekar return -ENOMEM;
265df6d737SAbhijeet Joglekar }
275df6d737SAbhijeet Joglekar }
285df6d737SAbhijeet Joglekar
295df6d737SAbhijeet Joglekar for (i = 0; i < blks; i++) {
305df6d737SAbhijeet Joglekar buf = rq->bufs[i];
315df6d737SAbhijeet Joglekar for (j = 0; j < VNIC_RQ_BUF_BLK_ENTRIES; j++) {
325df6d737SAbhijeet Joglekar buf->index = i * VNIC_RQ_BUF_BLK_ENTRIES + j;
335df6d737SAbhijeet Joglekar buf->desc = (u8 *)rq->ring.descs +
345df6d737SAbhijeet Joglekar rq->ring.desc_size * buf->index;
355df6d737SAbhijeet Joglekar if (buf->index + 1 == count) {
365df6d737SAbhijeet Joglekar buf->next = rq->bufs[0];
375df6d737SAbhijeet Joglekar break;
385df6d737SAbhijeet Joglekar } else if (j + 1 == VNIC_RQ_BUF_BLK_ENTRIES) {
395df6d737SAbhijeet Joglekar buf->next = rq->bufs[i + 1];
405df6d737SAbhijeet Joglekar } else {
415df6d737SAbhijeet Joglekar buf->next = buf + 1;
425df6d737SAbhijeet Joglekar buf++;
435df6d737SAbhijeet Joglekar }
445df6d737SAbhijeet Joglekar }
455df6d737SAbhijeet Joglekar }
465df6d737SAbhijeet Joglekar
475df6d737SAbhijeet Joglekar rq->to_use = rq->to_clean = rq->bufs[0];
485df6d737SAbhijeet Joglekar rq->buf_index = 0;
495df6d737SAbhijeet Joglekar
505df6d737SAbhijeet Joglekar return 0;
515df6d737SAbhijeet Joglekar }
525df6d737SAbhijeet Joglekar
vnic_rq_free(struct vnic_rq * rq)535df6d737SAbhijeet Joglekar void vnic_rq_free(struct vnic_rq *rq)
545df6d737SAbhijeet Joglekar {
555df6d737SAbhijeet Joglekar struct vnic_dev *vdev;
565df6d737SAbhijeet Joglekar unsigned int i;
575df6d737SAbhijeet Joglekar
585df6d737SAbhijeet Joglekar vdev = rq->vdev;
595df6d737SAbhijeet Joglekar
605df6d737SAbhijeet Joglekar vnic_dev_free_desc_ring(vdev, &rq->ring);
615df6d737SAbhijeet Joglekar
625df6d737SAbhijeet Joglekar for (i = 0; i < VNIC_RQ_BUF_BLKS_MAX; i++) {
635df6d737SAbhijeet Joglekar kfree(rq->bufs[i]);
645df6d737SAbhijeet Joglekar rq->bufs[i] = NULL;
655df6d737SAbhijeet Joglekar }
665df6d737SAbhijeet Joglekar
675df6d737SAbhijeet Joglekar rq->ctrl = NULL;
685df6d737SAbhijeet Joglekar }
695df6d737SAbhijeet Joglekar
vnic_rq_alloc(struct vnic_dev * vdev,struct vnic_rq * rq,unsigned int index,unsigned int desc_count,unsigned int desc_size)705df6d737SAbhijeet Joglekar int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
715df6d737SAbhijeet Joglekar unsigned int desc_count, unsigned int desc_size)
725df6d737SAbhijeet Joglekar {
735df6d737SAbhijeet Joglekar int err;
745df6d737SAbhijeet Joglekar
755df6d737SAbhijeet Joglekar rq->index = index;
765df6d737SAbhijeet Joglekar rq->vdev = vdev;
775df6d737SAbhijeet Joglekar
785df6d737SAbhijeet Joglekar rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index);
795df6d737SAbhijeet Joglekar if (!rq->ctrl) {
805df6d737SAbhijeet Joglekar printk(KERN_ERR "Failed to hook RQ[%d] resource\n", index);
815df6d737SAbhijeet Joglekar return -EINVAL;
825df6d737SAbhijeet Joglekar }
835df6d737SAbhijeet Joglekar
845df6d737SAbhijeet Joglekar vnic_rq_disable(rq);
855df6d737SAbhijeet Joglekar
865df6d737SAbhijeet Joglekar err = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size);
875df6d737SAbhijeet Joglekar if (err)
885df6d737SAbhijeet Joglekar return err;
895df6d737SAbhijeet Joglekar
905df6d737SAbhijeet Joglekar err = vnic_rq_alloc_bufs(rq);
915df6d737SAbhijeet Joglekar if (err) {
925df6d737SAbhijeet Joglekar vnic_rq_free(rq);
935df6d737SAbhijeet Joglekar return err;
945df6d737SAbhijeet Joglekar }
955df6d737SAbhijeet Joglekar
965df6d737SAbhijeet Joglekar return 0;
975df6d737SAbhijeet Joglekar }
985df6d737SAbhijeet Joglekar
vnic_rq_init(struct vnic_rq * rq,unsigned int cq_index,unsigned int error_interrupt_enable,unsigned int error_interrupt_offset)995df6d737SAbhijeet Joglekar void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
1005df6d737SAbhijeet Joglekar unsigned int error_interrupt_enable,
1015df6d737SAbhijeet Joglekar unsigned int error_interrupt_offset)
1025df6d737SAbhijeet Joglekar {
1035df6d737SAbhijeet Joglekar u64 paddr;
1045df6d737SAbhijeet Joglekar u32 fetch_index;
1055df6d737SAbhijeet Joglekar
1065df6d737SAbhijeet Joglekar paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET;
1075df6d737SAbhijeet Joglekar writeq(paddr, &rq->ctrl->ring_base);
1085df6d737SAbhijeet Joglekar iowrite32(rq->ring.desc_count, &rq->ctrl->ring_size);
1095df6d737SAbhijeet Joglekar iowrite32(cq_index, &rq->ctrl->cq_index);
1105df6d737SAbhijeet Joglekar iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable);
1115df6d737SAbhijeet Joglekar iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset);
1125df6d737SAbhijeet Joglekar iowrite32(0, &rq->ctrl->dropped_packet_count);
1135df6d737SAbhijeet Joglekar iowrite32(0, &rq->ctrl->error_status);
1145df6d737SAbhijeet Joglekar
1155df6d737SAbhijeet Joglekar /* Use current fetch_index as the ring starting point */
1165df6d737SAbhijeet Joglekar fetch_index = ioread32(&rq->ctrl->fetch_index);
1175df6d737SAbhijeet Joglekar rq->to_use = rq->to_clean =
1185df6d737SAbhijeet Joglekar &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
1195df6d737SAbhijeet Joglekar [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
1205df6d737SAbhijeet Joglekar iowrite32(fetch_index, &rq->ctrl->posted_index);
1215df6d737SAbhijeet Joglekar
1225df6d737SAbhijeet Joglekar rq->buf_index = 0;
1235df6d737SAbhijeet Joglekar }
1245df6d737SAbhijeet Joglekar
vnic_rq_error_status(struct vnic_rq * rq)1255df6d737SAbhijeet Joglekar unsigned int vnic_rq_error_status(struct vnic_rq *rq)
1265df6d737SAbhijeet Joglekar {
1275df6d737SAbhijeet Joglekar return ioread32(&rq->ctrl->error_status);
1285df6d737SAbhijeet Joglekar }
1295df6d737SAbhijeet Joglekar
vnic_rq_enable(struct vnic_rq * rq)1305df6d737SAbhijeet Joglekar void vnic_rq_enable(struct vnic_rq *rq)
1315df6d737SAbhijeet Joglekar {
1325df6d737SAbhijeet Joglekar iowrite32(1, &rq->ctrl->enable);
1335df6d737SAbhijeet Joglekar }
1345df6d737SAbhijeet Joglekar
vnic_rq_disable(struct vnic_rq * rq)1355df6d737SAbhijeet Joglekar int vnic_rq_disable(struct vnic_rq *rq)
1365df6d737SAbhijeet Joglekar {
1375df6d737SAbhijeet Joglekar unsigned int wait;
1385df6d737SAbhijeet Joglekar
1395df6d737SAbhijeet Joglekar iowrite32(0, &rq->ctrl->enable);
1405df6d737SAbhijeet Joglekar
1415df6d737SAbhijeet Joglekar /* Wait for HW to ACK disable request */
1425df6d737SAbhijeet Joglekar for (wait = 0; wait < 100; wait++) {
1435df6d737SAbhijeet Joglekar if (!(ioread32(&rq->ctrl->running)))
1445df6d737SAbhijeet Joglekar return 0;
1455df6d737SAbhijeet Joglekar udelay(1);
1465df6d737SAbhijeet Joglekar }
1475df6d737SAbhijeet Joglekar
1485df6d737SAbhijeet Joglekar printk(KERN_ERR "Failed to disable RQ[%d]\n", rq->index);
1495df6d737SAbhijeet Joglekar
1505df6d737SAbhijeet Joglekar return -ETIMEDOUT;
1515df6d737SAbhijeet Joglekar }
1525df6d737SAbhijeet Joglekar
vnic_rq_clean(struct vnic_rq * rq,void (* buf_clean)(struct vnic_rq * rq,struct vnic_rq_buf * buf))1535df6d737SAbhijeet Joglekar void vnic_rq_clean(struct vnic_rq *rq,
1545df6d737SAbhijeet Joglekar void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf))
1555df6d737SAbhijeet Joglekar {
1565df6d737SAbhijeet Joglekar struct vnic_rq_buf *buf;
1575df6d737SAbhijeet Joglekar u32 fetch_index;
1585df6d737SAbhijeet Joglekar
1595271d1caSSatish Kharat WARN_ON(ioread32(&rq->ctrl->enable));
1605df6d737SAbhijeet Joglekar
1615df6d737SAbhijeet Joglekar buf = rq->to_clean;
1625df6d737SAbhijeet Joglekar
1635df6d737SAbhijeet Joglekar while (vnic_rq_desc_used(rq) > 0) {
1645df6d737SAbhijeet Joglekar
1655df6d737SAbhijeet Joglekar (*buf_clean)(rq, buf);
1665df6d737SAbhijeet Joglekar
1675df6d737SAbhijeet Joglekar buf = rq->to_clean = buf->next;
1685df6d737SAbhijeet Joglekar rq->ring.desc_avail++;
1695df6d737SAbhijeet Joglekar }
1705df6d737SAbhijeet Joglekar
1715df6d737SAbhijeet Joglekar /* Use current fetch_index as the ring starting point */
1725df6d737SAbhijeet Joglekar fetch_index = ioread32(&rq->ctrl->fetch_index);
1735df6d737SAbhijeet Joglekar rq->to_use = rq->to_clean =
1745df6d737SAbhijeet Joglekar &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
1755df6d737SAbhijeet Joglekar [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
1765df6d737SAbhijeet Joglekar iowrite32(fetch_index, &rq->ctrl->posted_index);
1775df6d737SAbhijeet Joglekar
1785df6d737SAbhijeet Joglekar rq->buf_index = 0;
1795df6d737SAbhijeet Joglekar
1805df6d737SAbhijeet Joglekar vnic_dev_clear_desc_ring(&rq->ring);
1815df6d737SAbhijeet Joglekar }
182