0001
0002
0003
0004
0005
0006
0007 #include <linux/errno.h>
0008 #include <linux/types.h>
0009 #include <linux/pci.h>
0010 #include <linux/delay.h>
0011 #include <linux/slab.h>
0012 #include "vnic_dev.h"
0013 #include "vnic_rq.h"
0014
0015 static int vnic_rq_alloc_bufs(struct vnic_rq *rq)
0016 {
0017 struct vnic_rq_buf *buf;
0018 unsigned int i, j, count = rq->ring.desc_count;
0019 unsigned int blks = VNIC_RQ_BUF_BLKS_NEEDED(count);
0020
0021 for (i = 0; i < blks; i++) {
0022 rq->bufs[i] = kzalloc(VNIC_RQ_BUF_BLK_SZ, GFP_ATOMIC);
0023 if (!rq->bufs[i]) {
0024 printk(KERN_ERR "Failed to alloc rq_bufs\n");
0025 return -ENOMEM;
0026 }
0027 }
0028
0029 for (i = 0; i < blks; i++) {
0030 buf = rq->bufs[i];
0031 for (j = 0; j < VNIC_RQ_BUF_BLK_ENTRIES; j++) {
0032 buf->index = i * VNIC_RQ_BUF_BLK_ENTRIES + j;
0033 buf->desc = (u8 *)rq->ring.descs +
0034 rq->ring.desc_size * buf->index;
0035 if (buf->index + 1 == count) {
0036 buf->next = rq->bufs[0];
0037 break;
0038 } else if (j + 1 == VNIC_RQ_BUF_BLK_ENTRIES) {
0039 buf->next = rq->bufs[i + 1];
0040 } else {
0041 buf->next = buf + 1;
0042 buf++;
0043 }
0044 }
0045 }
0046
0047 rq->to_use = rq->to_clean = rq->bufs[0];
0048 rq->buf_index = 0;
0049
0050 return 0;
0051 }
0052
0053 void vnic_rq_free(struct vnic_rq *rq)
0054 {
0055 struct vnic_dev *vdev;
0056 unsigned int i;
0057
0058 vdev = rq->vdev;
0059
0060 vnic_dev_free_desc_ring(vdev, &rq->ring);
0061
0062 for (i = 0; i < VNIC_RQ_BUF_BLKS_MAX; i++) {
0063 kfree(rq->bufs[i]);
0064 rq->bufs[i] = NULL;
0065 }
0066
0067 rq->ctrl = NULL;
0068 }
0069
0070 int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
0071 unsigned int desc_count, unsigned int desc_size)
0072 {
0073 int err;
0074
0075 rq->index = index;
0076 rq->vdev = vdev;
0077
0078 rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index);
0079 if (!rq->ctrl) {
0080 printk(KERN_ERR "Failed to hook RQ[%d] resource\n", index);
0081 return -EINVAL;
0082 }
0083
0084 vnic_rq_disable(rq);
0085
0086 err = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size);
0087 if (err)
0088 return err;
0089
0090 err = vnic_rq_alloc_bufs(rq);
0091 if (err) {
0092 vnic_rq_free(rq);
0093 return err;
0094 }
0095
0096 return 0;
0097 }
0098
0099 void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
0100 unsigned int error_interrupt_enable,
0101 unsigned int error_interrupt_offset)
0102 {
0103 u64 paddr;
0104 u32 fetch_index;
0105
0106 paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET;
0107 writeq(paddr, &rq->ctrl->ring_base);
0108 iowrite32(rq->ring.desc_count, &rq->ctrl->ring_size);
0109 iowrite32(cq_index, &rq->ctrl->cq_index);
0110 iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable);
0111 iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset);
0112 iowrite32(0, &rq->ctrl->dropped_packet_count);
0113 iowrite32(0, &rq->ctrl->error_status);
0114
0115
0116 fetch_index = ioread32(&rq->ctrl->fetch_index);
0117 rq->to_use = rq->to_clean =
0118 &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
0119 [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
0120 iowrite32(fetch_index, &rq->ctrl->posted_index);
0121
0122 rq->buf_index = 0;
0123 }
0124
0125 unsigned int vnic_rq_error_status(struct vnic_rq *rq)
0126 {
0127 return ioread32(&rq->ctrl->error_status);
0128 }
0129
0130 void vnic_rq_enable(struct vnic_rq *rq)
0131 {
0132 iowrite32(1, &rq->ctrl->enable);
0133 }
0134
0135 int vnic_rq_disable(struct vnic_rq *rq)
0136 {
0137 unsigned int wait;
0138
0139 iowrite32(0, &rq->ctrl->enable);
0140
0141
0142 for (wait = 0; wait < 100; wait++) {
0143 if (!(ioread32(&rq->ctrl->running)))
0144 return 0;
0145 udelay(1);
0146 }
0147
0148 printk(KERN_ERR "Failed to disable RQ[%d]\n", rq->index);
0149
0150 return -ETIMEDOUT;
0151 }
0152
0153 void vnic_rq_clean(struct vnic_rq *rq,
0154 void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf))
0155 {
0156 struct vnic_rq_buf *buf;
0157 u32 fetch_index;
0158
0159 WARN_ON(ioread32(&rq->ctrl->enable));
0160
0161 buf = rq->to_clean;
0162
0163 while (vnic_rq_desc_used(rq) > 0) {
0164
0165 (*buf_clean)(rq, buf);
0166
0167 buf = rq->to_clean = buf->next;
0168 rq->ring.desc_avail++;
0169 }
0170
0171
0172 fetch_index = ioread32(&rq->ctrl->fetch_index);
0173 rq->to_use = rq->to_clean =
0174 &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES]
0175 [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES];
0176 iowrite32(fetch_index, &rq->ctrl->posted_index);
0177
0178 rq->buf_index = 0;
0179
0180 vnic_dev_clear_desc_ring(&rq->ring);
0181 }