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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "muser.h"
#include "cap.h"
struct cap {
uint8_t start;
uint8_t end;
uint8_t id;
lm_cap_access_t *fn;
};
struct caps {
struct cap caps[LM_MAX_CAPS];
int nr_caps;
};
/*
* Tells whether a capability is being accessed.
*/
static bool
cap_is_accessed(struct cap *caps, int nr_caps, loff_t offset)
{
/*
* Ignore if it's at the standard PCI header. The first capability starts
* right after that.
*/
if (offset < PCI_STD_HEADER_SIZEOF) {
return false;
}
/* ignore if there are no capabilities */
if (!nr_caps) {
return false;
}
assert(caps);
/*
* Ignore if it's before the first capability. This check is probably
* redundant since we assume that the first capability starts right after
* the standard PCI header.
* TODO should we check that it doesn't cross into the first capability?
*/
if (offset < caps[0].start) {
return false;
}
/* ignore if it's past the last capability */
if (offset > caps[nr_caps - 1].end) {
return false;
}
return true;
}
/*
* Returns the PCI capability that is contained within the specified region
* (offset + count).
*/
static struct cap*
cap_find(struct cap *caps, int nr_caps, loff_t offset, size_t count)
{
struct cap *cap;
bool found = false;
cap = caps;
while (cap < caps + nr_caps) {
/*
* TODO this assumes that at most one capability is read. It might be
* legitimate to read an arbitrary number of bytes, which we could
* support. For now lets explicitly fail such cases.
*/
if (offset >= cap->start && offset + count - 1 <= cap->end) {
return cap;
}
cap++;
}
/* this means that the access spans more than a capability */
return NULL;
}
/*
* Tells whether the header of a PCI capability is accessed.
*/
static bool
cap_header_is_accessed(struct cap *cap, loff_t offset)
{
assert(cap);
return offset - cap->start <= 1;
}
/*
* Reads the header of a PCI capability.
*/
static int
cap_header_access(struct caps *caps, struct cap *cap, char *buf,
loff_t offset, size_t count, bool is_write)
{
int n;
/*
* We don't allow ID and next to be written. TODO not sure what the PCI
* spec says about this, need to check.
*/
if (is_write) {
return -EINVAL;
}
assert(caps);
assert(cap);
n = 0;
/*
* We handle reads to ID and next, the rest is handled by the callback.
*/
if (offset == cap->start && count > 0) { /* ID */
buf[n++] = cap->id;
offset++;
count--;
}
if (offset == cap->start + 1 && count > 0) { /* next */
if ((cap - caps->caps) / sizeof *cap == caps->nr_caps - 1) {
buf[n++] = 0;
} else {
buf[n++] = (cap + 1)->start;
}
offset++;
count--;
}
return n;
}
ssize_t
cap_maybe_access(struct caps *caps, void *pvt, char *buf, size_t count,
loff_t offset, bool is_write)
{
bool found;
struct cap *cap;
int n;
if (!caps) {
return 0;
}
if (!count) {
return 0;
}
if (!cap_is_accessed(caps->caps, caps->nr_caps, offset)) {
return 0;
}
/* we're now guaranteed that the access is within some capability */
cap = cap_find(caps->caps, caps->nr_caps, offset, count);
if (!cap) {
return 0;
}
if (cap_header_is_accessed(cap, offset)) {
return cap_header_access(caps, cap, buf, offset, count, is_write);
}
if (count > 0) {
return cap->fn(pvt, cap->id, buf, count, offset - cap->start, is_write);
}
return 0;
}
static bool
cap_is_valid(uint8_t id)
{
return id >= PCI_CAP_ID_PM && id <= PCI_CAP_ID_MAX;
}
struct caps*
caps_create(lm_cap_t *lm_caps, int nr_caps)
{
uint8_t prev_end;
int i, err = 0;
struct caps *caps = NULL;
if (nr_caps <= 0 || nr_caps >= LM_MAX_CAPS) {
err = EINVAL;
goto out;
}
assert(lm_caps);
caps = calloc(1, sizeof *caps);
if (!caps) {
err = errno;
goto out;
}
prev_end = PCI_STD_HEADER_SIZEOF - 1;
for (i = 0; i < nr_caps; i++) {
if (!cap_is_valid(lm_caps[i].id) || !lm_caps[i].fn || !lm_caps[i].size) {
err = EINVAL;
goto out;
}
caps->caps[i].id = lm_caps[i].id;
caps->caps[i].fn = lm_caps[i].fn;
/* FIXME PCI capabilities must be dword aligned. */
caps->caps[i].start = prev_end + 1;
caps->caps[i].end = prev_end = caps->caps[i].start + lm_caps[i].size - 1;
}
caps->nr_caps = nr_caps;
out:
if (err) {
free(caps);
caps = NULL;
}
return caps;
}
/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
|