-
Notifications
You must be signed in to change notification settings - Fork 6
/
pru_vring.c
144 lines (114 loc) · 3.32 KB
/
pru_vring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* pru_vring.c - VRING support functions
*/
#include "pru_vring.h"
void pru_vring_init(struct pru_vring *pvr, const char *name,
const struct fw_rsc_vdev_vring *rsc_vring)
{
vring_init(&pvr->vr, rsc_vring->num,
(void *)rsc_vring->da, rsc_vring->align);
pvr->align = rsc_vring->align;
pvr->notifyid = rsc_vring->notifyid;
/* verify it's a power of two */
BUG_ON(rsc_vring->num & (rsc_vring->num - 1));
pvr->num_mask = rsc_vring->num - 1;
pvr->last_avail = 0;
pvr->last_avail_orig = 0;
pvr->avail_idx_p = &pvr->vr.avail->idx;
pvr->used_idx_p = &pvr->vr.used->idx;
pvr->avail_ring_p = pvr->vr.avail->ring;
}
void pru_vring_elem_init(struct pru_vring *pvr,
struct pru_vring_elem *pvre)
{
u16 num = pvr->vr.num;
pvre->idx = num;
pvre->out_num = 0;
pvre->out_idx = num;
pvre->out_len = 0;
pvre->in_num = 0;
pvre->in_idx = num;
pvre->in_len = 0;
/* save in case of rollback */
pvr->last_avail_orig = pvr->last_avail;
}
int pru_vring_pop(struct pru_vring *pvr, struct pru_vring_elem *pvre)
{
u16 i, head;
struct vring_desc *vrd;
if (!pru_vring_num_heads(pvr, pvr->last_avail))
return 0;
pru_vring_elem_init(pvr, pvre);
/* get head */
head = pru_vring_get_head(pvr, pvr->last_avail++);
// if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))
// pru_vring_avail_event(pvr, pru_vring_avail_idx(pvr));
vrd = pru_vring_desc(pvr, head);
/* we don't support VRING_DESC_F_INDIRECT */
BUG_ON(vrd->flags & VRING_DESC_F_INDIRECT);
i = head;
pvre->idx = i;
/* loop collecting elements */
for (;;) {
if (vrd->flags & VRING_DESC_F_WRITE) {
if (pvre->in_num++ == 0)
pvre->in_idx = i & pvr->num_mask;
pvre->in_len += vrd->len;
} else {
if (pvre->out_num++ == 0)
pvre->out_idx = i & pvr->num_mask;
pvre->out_len += vrd->len;
}
if (!(vrd->flags & VRING_DESC_F_NEXT))
break;
/* next descriptor */
vrd = pru_vring_desc(pvr, ++i);
}
return pvre->in_num + pvre->out_num;
}
void pru_vring_push(struct pru_vring *pvr,
const struct pru_vring_elem *pvre, u32 len)
{
u16 idx, old;
struct vring_used_elem *used_vring;
/* first fill */
old = pru_vring_used_idx(pvr);
idx = old & pvr->num_mask;
used_vring = &pvr->vr.used->ring[idx];
used_vring->id = pvre->idx & pvr->num_mask;
used_vring->len = len;
/* now update */
pru_vring_used_idx_set(pvr, ++old);
}
#ifdef DEBUG
void dump_vring(const char *name, struct vring *vring, unsigned int align)
{
struct vring_desc *vd;
int i, sz;
sz = vring_size(vring->num, align);
sc_printf("vring %s @0x%x num %d size %d desc=0x%x avail=0x%x used=0x%x",
name, (unsigned int)vring->desc, vring->num, sz,
(unsigned int)vring->desc, (unsigned int)vring->avail,
(unsigned int)vring->used);
for (i = 0; i < vring->num; i++) {
vd = &vring->desc[i];
sc_printf(" d#%d: a 0x%x l 0x%x f 0x%x n 0x%x",
i, (__u32)vd->addr, vd->len, vd->flags, vd->next);
}
sc_printf(" avail: flags 0x%x idx 0x%x",
vring->avail->flags, vring->avail->idx);
for (i = 0; i < vring->num; i++) {
sc_printf(" a#%d: ring 0x%x", i,
vring->avail->ring[i]);
}
sc_printf(" avail: used_event_idx 0x%x",
vring->avail->ring[vring->num]);
sc_printf(" used: flags 0x%x idx 0x%x",
vring->used->flags, vring->used->idx);
for (i = 0; i < vring->num; i++) {
sc_printf(" u#%d: id 0x%x len 0x%x", i,
vring->used->ring[i].id,
vring->used->ring[i].len);
}
}
#endif