-
Notifications
You must be signed in to change notification settings - Fork 30
/
aq_sysfs.c
326 lines (273 loc) · 7.93 KB
/
aq_sysfs.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// SPDX-License-Identifier: GPL-2.0-only
/* Atlantic Network Driver
*
* Copyright (C) 2018-2019 aQuantia Corporation
* Copyright (C) 2019-2020 Marvell International Ltd.
*/
#include "aq_nic.h"
#include "aq_sysfs.h"
#define to_bar(obj) container_of((obj), struct aq_tsn_bar, kobj)
struct bar_sysfs_entry {
struct attribute attr;
ssize_t (*show)(struct aq_tsn_bar *, char *);
ssize_t (*store)(struct aq_tsn_bar *, char*, size_t);
};
#define aqdev_bar_attr(_field, _fmt) \
static ssize_t \
bar_##_field##_show(struct aq_tsn_bar *bar, char *buf) \
{ \
return sprintf(buf, _fmt, bar->_field); \
} \
static struct bar_sysfs_entry bar_##_field##_attr = \
__ATTR(_field, S_IRUGO, bar_##_field##_show, NULL);
#pragma GCC diagnostic ignored "-Wformat"
aqdev_bar_attr(addr, "0x%lx\n");
aqdev_bar_attr(len, "0x%lx\n");
aqdev_bar_attr(index, "%d\n");
#pragma GCC diagnostic warning "-Wformat"
static ssize_t bar_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct aq_tsn_bar *bar = to_bar(kobj);
struct bar_sysfs_entry *entry =
container_of(attr, struct bar_sysfs_entry, attr);
if (!entry->show)
return -EIO;
return entry->show(bar, buf);
}
static const struct sysfs_ops bar_ops = {
.show = bar_attr_show,
};
static void bar_release(struct kobject *kobj)
{
struct aq_tsn_bar *bar = to_bar(kobj);
kfree(bar);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
static struct attribute *bar_attrs[] = {
&bar_addr_attr.attr,
&bar_len_attr.attr,
&bar_index_attr.attr,
NULL,
};
#else
#define BAR_ATTRIBUTE_GROUPS(_name) \
struct attribute *bar##_name##_attr = &bar_##_name##_attr.attr; \
static const struct attribute_group bar_##_name##_group = { \
.attrs = &bar##_name##_attr, \
};
BAR_ATTRIBUTE_GROUPS(addr);
BAR_ATTRIBUTE_GROUPS(len);
BAR_ATTRIBUTE_GROUPS(index);
const struct attribute_group *bar_attrs_grp[] = {
&bar_addr_group,
&bar_len_group,
&bar_index_group,
NULL,
};
#endif
static struct kobj_type bar_type = {
.sysfs_ops = &bar_ops,
.release = bar_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
.default_attrs = bar_attrs,
#else
.default_groups = bar_attrs_grp,
#endif
};
static void aq_release_bars(struct aq_tsn_s *self)
{
int i;
for (i = 0; i < PCI_ROM_RESOURCE; i++) {
struct aq_tsn_bar *bar = self->sysfs_bar[i];
if (!bar)
continue;
kobject_del(&bar->kobj);
kobject_put(&bar->kobj);
}
kobject_del(self->sysfs_bars);
kobject_put(self->sysfs_bars);
}
static int aq_create_bars(struct aq_tsn_s *self)
{
int ret;
int mask = pci_select_bars(self->aq_nic->pdev, IORESOURCE_MEM | IORESOURCE_IO);
int i;
struct aq_tsn_bar *bar;
ret = -EINVAL;
self->sysfs_bars = kobject_create_and_add("bars", &aq_nic_get_dev(self->aq_nic)->kobj);
if (!self->sysfs_bars)
goto err;
for (i = 0; mask && i < PCI_ROM_RESOURCE; mask >>= 1, i++){
const char *pref = pci_resource_flags(self->aq_nic->pdev, i) & IORESOURCE_MEM ? "mem" : "io";
if (!(mask & 1))
continue;
ret = -ENOMEM;
bar = kzalloc(sizeof(*bar), GFP_KERNEL);
if (!bar)
goto err;
kobject_init(&bar->kobj, &bar_type);
bar->index = i;
bar->addr = pci_resource_start(self->aq_nic->pdev, i);
bar->len = pci_resource_len(self->aq_nic->pdev, i);
self->sysfs_bar[i] = bar;
ret = kobject_add(&bar->kobj, self->sysfs_bars, "%sbar%d", pref, i);
if (ret)
goto err;
}
return 0;
err:
aq_release_bars(self);
return ret;
}
int aq_create_attrs(struct aq_tsn_s *self)
{
int ret = -EINVAL;
self->sysfs_mem = kobject_create_and_add("mem", &aq_nic_get_dev(self->aq_nic)->kobj);
if (!self->sysfs_mem)
goto err_mem;
ret = aq_create_bars(self);
if (ret)
goto err_bar;
return 0;
err_bar:
kobject_del(self->sysfs_mem);
kobject_put(self->sysfs_mem);
err_mem:
dev_err(aq_nic_get_dev(self->aq_nic), "Couldn't create sysfs files: %d\n", ret);
return ret;
}
void aq_del_attrs(struct aq_tsn_s *self)
{
aq_release_bars(self);
kobject_del(self->sysfs_mem);
kobject_put(self->sysfs_mem);
}
#define to_memreg(obj) container_of(obj, struct aq_memreg, kobj)
struct memreg_sysfs_entry {
struct attribute attr;
ssize_t (*show)(struct aq_memreg *, char *);
ssize_t (*store)(struct aq_memreg *, char*, size_t);
};
#define aqdev_memreg_attr(_field, _fmt) \
static ssize_t \
memreg_##_field##_show(struct aq_memreg *memreg, char *buf) \
{ \
return sprintf(buf, _fmt, memreg->_field); \
} \
static struct memreg_sysfs_entry memreg_##_field##_attr = \
__ATTR(_field, S_IRUGO, memreg_##_field##_show, NULL);
#pragma GCC diagnostic ignored "-Wformat"
aqdev_memreg_attr(vaddr, "0x%lx\n");
aqdev_memreg_attr(paddr, "0x%lx\n");
aqdev_memreg_attr(size, "0x%x\n");
aqdev_memreg_attr(real_size, "0x%x\n");
aqdev_memreg_attr(index, "%d\n");
#pragma GCC diagnostic warning "-Wformat"
static ssize_t memreg_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct aq_memreg *memreg = to_memreg(kobj);
struct memreg_sysfs_entry *entry =
container_of(attr, struct memreg_sysfs_entry, attr);
if (!entry->show)
return -EIO;
return entry->show(memreg, buf);
}
static const struct sysfs_ops memreg_ops = {
.show = memreg_attr_show,
};
static void memreg_release(struct kobject *kobj)
{
struct aq_memreg *memreg = to_memreg(kobj);
kfree(memreg);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
static struct attribute *memreg_attrs[] = {
&memreg_vaddr_attr.attr,
&memreg_paddr_attr.attr,
&memreg_size_attr.attr,
&memreg_real_size_attr.attr,
&memreg_index_attr.attr,
NULL,
};
#else
#define MEMREG_ATTRIBUTE_GROUPS(_name) \
struct attribute *memreg##_name##_attr = &memreg_##_name##_attr.attr; \
static const struct attribute_group memreg_##_name##_group = { \
.attrs = &memreg##_name##_attr, \
};
MEMREG_ATTRIBUTE_GROUPS(vaddr);
MEMREG_ATTRIBUTE_GROUPS(paddr);
MEMREG_ATTRIBUTE_GROUPS(size);
MEMREG_ATTRIBUTE_GROUPS(real_size);
MEMREG_ATTRIBUTE_GROUPS(index);
static const struct attribute_group *memreg_attrs_grp[] = {
&memreg_vaddr_group,
&memreg_paddr_group,
&memreg_size_group,
&memreg_real_size_group,
&memreg_index_group,
NULL,
};
#endif
struct kobj_type memreg_type = {
.sysfs_ops = &memreg_ops,
.release = memreg_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 2, 0)
.default_attrs = memreg_attrs,
#else
.default_groups = memreg_attrs_grp,
#endif
};
static int memreg_mmap(struct file *file, struct kobject *kobj, struct bin_attribute *attr,
struct vm_area_struct *vma)
{
struct aq_memreg *memreg = attr->private;
unsigned long requested = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
unsigned long pages = (unsigned long)memreg->real_size >> PAGE_SHIFT;
if (vma->vm_pgoff + requested > pages)
return -EINVAL;
#if defined(__arm__) || defined(__aarch64__)
// had issues with writes to descriptors/packets not being seen by HW for arm systems. this function seemed to fix this
#ifdef pgprot_dmacoherent
vma->vm_page_pgprot = prot_dmacoherent(vma->vm_page_prot);
#else //!defined(pgprot_dmacoherent)
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
//vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
#endif //defined(pgprot_dmacoherent)
#endif
if (remap_pfn_range(vma, vma->vm_start, memreg->paddr >> PAGE_SHIFT,
vma->vm_end - vma->vm_start, vma->vm_page_prot))
return -EAGAIN;
return 0;
}
int aq_publish_memreg(struct aq_memreg *memreg)
{
int ret;
struct bin_attribute *mmap_attr = &memreg->mmap_attr;
ret = kobject_add(&memreg->kobj, memreg->aq_tsn->sysfs_mem,
"%d", memreg->index);
if (ret)
goto err_add;
mmap_attr->mmap = memreg_mmap;
mmap_attr->attr.name = "mmap";
mmap_attr->attr.mode = S_IRUSR | S_IWUSR;
mmap_attr->size = memreg->real_size;
mmap_attr->private = memreg;
ret = sysfs_create_bin_file(&memreg->kobj, mmap_attr);
if (ret)
goto err_map_add;
return 0;
err_map_add:
kobject_del(&memreg->kobj);
err_add:
kobject_put(&memreg->kobj);
return ret;
}
void aq_hide_memreg(struct aq_memreg *memreg)
{
sysfs_remove_bin_file(&memreg->kobj, &memreg->mmap_attr);
kobject_del(&memreg->kobj);
kobject_put(&memreg->kobj);
}