forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdma_cdev.c
624 lines (537 loc) · 14.4 KB
/
xdma_cdev.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*******************************************************************************
*
* Xilinx XDMA IP Core Linux Driver
* Copyright(c) 2015 - 2020 Xilinx, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The full GNU General Public License is included in this distribution in
* the file called "LICENSE".
*
* Karen Xie <[email protected]>
*
******************************************************************************/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
#include "xdma_cdev.h"
static struct class *g_xdma_class;
struct kmem_cache *cdev_cache;
enum cdev_type {
CHAR_USER,
CHAR_CTRL,
CHAR_XVC,
CHAR_EVENTS,
CHAR_XDMA_H2C,
CHAR_XDMA_C2H,
CHAR_BYPASS_H2C,
CHAR_BYPASS_C2H,
CHAR_BYPASS,
};
static const char * const devnode_names[] = {
XDMA_NODE_NAME "%d_user",
XDMA_NODE_NAME "%d_control",
XDMA_NODE_NAME "%d_xvc",
XDMA_NODE_NAME "%d_events_%d",
XDMA_NODE_NAME "%d_h2c_%d",
XDMA_NODE_NAME "%d_c2h_%d",
XDMA_NODE_NAME "%d_bypass_h2c_%d",
XDMA_NODE_NAME "%d_bypass_c2h_%d",
XDMA_NODE_NAME "%d_bypass",
};
enum xpdev_flags_bits {
XDF_CDEV_USER,
XDF_CDEV_CTRL,
XDF_CDEV_XVC,
XDF_CDEV_EVENT,
XDF_CDEV_SG,
XDF_CDEV_BYPASS,
};
static inline void xpdev_flag_set(struct xdma_pci_dev *xpdev,
enum xpdev_flags_bits fbit)
{
xpdev->flags |= 1 << fbit;
}
static inline void xcdev_flag_clear(struct xdma_pci_dev *xpdev,
enum xpdev_flags_bits fbit)
{
xpdev->flags &= ~(1 << fbit);
}
static inline int xpdev_flag_test(struct xdma_pci_dev *xpdev,
enum xpdev_flags_bits fbit)
{
return xpdev->flags & (1 << fbit);
}
#ifdef __XDMA_SYSFS__
ssize_t xdma_dev_instance_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct xdma_pci_dev *xpdev =
(struct xdma_pci_dev *)dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\t%d\n",
xpdev->major, xpdev->xdev->idx);
}
static DEVICE_ATTR_RO(xdma_dev_instance);
#endif
static int config_kobject(struct xdma_cdev *xcdev, enum cdev_type type)
{
int rv = -EINVAL;
struct xdma_dev *xdev = xcdev->xdev;
struct xdma_engine *engine = xcdev->engine;
switch (type) {
case CHAR_XDMA_H2C:
case CHAR_XDMA_C2H:
case CHAR_BYPASS_H2C:
case CHAR_BYPASS_C2H:
if (!engine) {
pr_err("Invalid DMA engine\n");
return rv;
}
rv = kobject_set_name(&xcdev->cdev.kobj, devnode_names[type],
xdev->idx, engine->channel);
break;
case CHAR_BYPASS:
case CHAR_USER:
case CHAR_CTRL:
case CHAR_XVC:
rv = kobject_set_name(&xcdev->cdev.kobj, devnode_names[type],
xdev->idx);
break;
case CHAR_EVENTS:
rv = kobject_set_name(&xcdev->cdev.kobj, devnode_names[type],
xdev->idx, xcdev->bar);
break;
default:
pr_warn("%s: UNKNOWN type 0x%x.\n", __func__, type);
break;
}
if (rv)
pr_err("%s: type 0x%x, failed %d.\n", __func__, type, rv);
return rv;
}
int xcdev_check(const char *fname, struct xdma_cdev *xcdev, bool check_engine)
{
struct xdma_dev *xdev;
if (!xcdev || xcdev->magic != MAGIC_CHAR) {
pr_info("%s, xcdev 0x%p, magic 0x%lx.\n",
fname, xcdev, xcdev ? xcdev->magic : 0xFFFFFFFF);
return -EINVAL;
}
xdev = xcdev->xdev;
if (!xdev || xdev->magic != MAGIC_DEVICE) {
pr_info("%s, xdev 0x%p, magic 0x%lx.\n",
fname, xdev, xdev ? xdev->magic : 0xFFFFFFFF);
return -EINVAL;
}
if (check_engine) {
struct xdma_engine *engine = xcdev->engine;
if (!engine || engine->magic != MAGIC_ENGINE) {
pr_info("%s, engine 0x%p, magic 0x%lx.\n", fname,
engine, engine ? engine->magic : 0xFFFFFFFF);
return -EINVAL;
}
}
return 0;
}
int char_open(struct inode *inode, struct file *file)
{
struct xdma_cdev *xcdev;
/* pointer to containing structure of the character device inode */
xcdev = container_of(inode->i_cdev, struct xdma_cdev, cdev);
if (xcdev->magic != MAGIC_CHAR) {
pr_err("xcdev 0x%p inode 0x%lx magic mismatch 0x%lx\n",
xcdev, inode->i_ino, xcdev->magic);
return -EINVAL;
}
/* create a reference to our char device in the opened file */
file->private_data = xcdev;
return 0;
}
/*
* Called when the device goes from used to unused.
*/
int char_close(struct inode *inode, struct file *file)
{
struct xdma_dev *xdev;
struct xdma_cdev *xcdev = (struct xdma_cdev *)file->private_data;
if (!xcdev) {
pr_err("char device with inode 0x%lx xcdev NULL\n",
inode->i_ino);
return -EINVAL;
}
if (xcdev->magic != MAGIC_CHAR) {
pr_err("xcdev 0x%p magic mismatch 0x%lx\n",
xcdev, xcdev->magic);
return -EINVAL;
}
/* fetch device specific data stored earlier during open */
xdev = xcdev->xdev;
if (!xdev) {
pr_err("char device with inode 0x%lx xdev NULL\n",
inode->i_ino);
return -EINVAL;
}
if (xdev->magic != MAGIC_DEVICE) {
pr_err("xdev 0x%p magic mismatch 0x%lx\n", xdev, xdev->magic);
return -EINVAL;
}
return 0;
}
/* create_xcdev() -- create a character device interface to data or control bus
*
* If at least one SG DMA engine is specified, the character device interface
* is coupled to the SG DMA file operations which operate on the data bus. If
* no engines are specified, the interface is coupled with the control bus.
*/
static int create_sys_device(struct xdma_cdev *xcdev, enum cdev_type type)
{
struct xdma_dev *xdev = xcdev->xdev;
struct xdma_engine *engine = xcdev->engine;
int last_param;
if (type == CHAR_EVENTS)
last_param = xcdev->bar;
else
last_param = engine ? engine->channel : 0;
xcdev->sys_device = device_create(g_xdma_class, &xdev->pdev->dev,
xcdev->cdevno, NULL, devnode_names[type], xdev->idx,
last_param);
if (!xcdev->sys_device) {
pr_err("device_create(%s) failed\n", devnode_names[type]);
return -1;
}
return 0;
}
static int destroy_xcdev(struct xdma_cdev *cdev)
{
if (!cdev) {
pr_warn("cdev NULL.\n");
return -EINVAL;
}
if (cdev->magic != MAGIC_CHAR) {
pr_warn("cdev 0x%p magic mismatch 0x%lx\n", cdev, cdev->magic);
return -EINVAL;
}
if (!cdev->xdev) {
pr_err("xdev NULL\n");
return -EINVAL;
}
if (!g_xdma_class) {
pr_err("g_xdma_class NULL\n");
return -EINVAL;
}
if (!cdev->sys_device) {
pr_err("cdev sys_device NULL\n");
return -EINVAL;
}
if (cdev->sys_device)
device_destroy(g_xdma_class, cdev->cdevno);
cdev_del(&cdev->cdev);
return 0;
}
static int create_xcdev(struct xdma_pci_dev *xpdev, struct xdma_cdev *xcdev,
int bar, struct xdma_engine *engine,
enum cdev_type type)
{
int rv;
int minor;
struct xdma_dev *xdev = xpdev->xdev;
dev_t dev;
spin_lock_init(&xcdev->lock);
/* new instance? */
if (!xpdev->major) {
/* allocate a dynamically allocated char device node */
int rv = alloc_chrdev_region(&dev, XDMA_MINOR_BASE,
XDMA_MINOR_COUNT, XDMA_NODE_NAME);
if (rv) {
pr_err("unable to allocate cdev region %d.\n", rv);
return rv;
}
xpdev->major = MAJOR(dev);
}
/*
* do not register yet, create kobjects and name them,
*/
xcdev->magic = MAGIC_CHAR;
xcdev->cdev.owner = THIS_MODULE;
xcdev->xpdev = xpdev;
xcdev->xdev = xdev;
xcdev->engine = engine;
xcdev->bar = bar;
rv = config_kobject(xcdev, type);
if (rv < 0)
return rv;
switch (type) {
case CHAR_USER:
case CHAR_CTRL:
/* minor number is type index for non-SGDMA interfaces */
minor = type;
cdev_ctrl_init(xcdev);
break;
case CHAR_XVC:
/* minor number is type index for non-SGDMA interfaces */
minor = type;
cdev_xvc_init(xcdev);
break;
case CHAR_XDMA_H2C:
minor = 32 + engine->channel;
cdev_sgdma_init(xcdev);
break;
case CHAR_XDMA_C2H:
minor = 36 + engine->channel;
cdev_sgdma_init(xcdev);
break;
case CHAR_EVENTS:
minor = 10 + bar;
cdev_event_init(xcdev);
break;
case CHAR_BYPASS_H2C:
minor = 64 + engine->channel;
cdev_bypass_init(xcdev);
break;
case CHAR_BYPASS_C2H:
minor = 68 + engine->channel;
cdev_bypass_init(xcdev);
break;
case CHAR_BYPASS:
minor = 100;
cdev_bypass_init(xcdev);
break;
default:
pr_info("type 0x%x NOT supported.\n", type);
return -EINVAL;
}
xcdev->cdevno = MKDEV(xpdev->major, minor);
/* bring character device live */
rv = cdev_add(&xcdev->cdev, xcdev->cdevno, 1);
if (rv < 0) {
pr_err("cdev_add failed %d, type 0x%x.\n", rv, type);
goto unregister_region;
}
dbg_init("xcdev 0x%p, %u:%u, %s, type 0x%x.\n",
xcdev, xpdev->major, minor, xcdev->cdev.kobj.name, type);
/* create device on our class */
if (g_xdma_class) {
rv = create_sys_device(xcdev, type);
if (rv < 0)
goto del_cdev;
}
return 0;
del_cdev:
cdev_del(&xcdev->cdev);
unregister_region:
unregister_chrdev_region(xcdev->cdevno, XDMA_MINOR_COUNT);
return rv;
}
void xpdev_destroy_interfaces(struct xdma_pci_dev *xpdev)
{
int i = 0;
int rv;
#ifdef __XDMA_SYSFS__
device_remove_file(&xpdev->pdev->dev, &dev_attr_xdma_dev_instance);
#endif
if (xpdev_flag_test(xpdev, XDF_CDEV_SG)) {
/* iterate over channels */
for (i = 0; i < xpdev->h2c_channel_max; i++) {
/* remove SG DMA character device */
rv = destroy_xcdev(&xpdev->sgdma_h2c_cdev[i]);
if (rv < 0)
pr_err("Failed to destroy h2c xcdev %d error :0x%x\n",
i, rv);
}
for (i = 0; i < xpdev->c2h_channel_max; i++) {
rv = destroy_xcdev(&xpdev->sgdma_c2h_cdev[i]);
if (rv < 0)
pr_err("Failed to destroy c2h xcdev %d error 0x%x\n",
i, rv);
}
}
if (xpdev_flag_test(xpdev, XDF_CDEV_EVENT)) {
for (i = 0; i < xpdev->user_max; i++) {
rv = destroy_xcdev(&xpdev->events_cdev[i]);
if (rv < 0)
pr_err("Failed to destroy cdev event %d error 0x%x\n",
i, rv);
}
}
/* remove control character device */
if (xpdev_flag_test(xpdev, XDF_CDEV_CTRL)) {
rv = destroy_xcdev(&xpdev->ctrl_cdev);
if (rv < 0)
pr_err("Failed to destroy cdev ctrl event %d error 0x%x\n",
i, rv);
}
/* remove user character device */
if (xpdev_flag_test(xpdev, XDF_CDEV_USER)) {
rv = destroy_xcdev(&xpdev->user_cdev);
if (rv < 0)
pr_err("Failed to destroy user cdev %d error 0x%x\n",
i, rv);
}
if (xpdev_flag_test(xpdev, XDF_CDEV_XVC)) {
rv = destroy_xcdev(&xpdev->xvc_cdev);
if (rv < 0)
pr_err("Failed to destroy xvc cdev %d error 0x%x\n",
i, rv);
}
if (xpdev_flag_test(xpdev, XDF_CDEV_BYPASS)) {
/* iterate over channels */
for (i = 0; i < xpdev->h2c_channel_max; i++) {
/* remove DMA Bypass character device */
rv = destroy_xcdev(&xpdev->bypass_h2c_cdev[i]);
if (rv < 0)
pr_err("Failed to destroy bypass h2c cdev %d error 0x%x\n",
i, rv);
}
for (i = 0; i < xpdev->c2h_channel_max; i++) {
rv = destroy_xcdev(&xpdev->bypass_c2h_cdev[i]);
if (rv < 0)
pr_err("Failed to destroy bypass c2h %d error 0x%x\n",
i, rv);
}
rv = destroy_xcdev(&xpdev->bypass_cdev_base);
if (rv < 0)
pr_err("Failed to destroy base cdev\n");
}
if (xpdev->major)
unregister_chrdev_region(
MKDEV(xpdev->major, XDMA_MINOR_BASE),
XDMA_MINOR_COUNT);
}
int xpdev_create_interfaces(struct xdma_pci_dev *xpdev)
{
struct xdma_dev *xdev = xpdev->xdev;
struct xdma_engine *engine;
int i;
int rv = 0;
/* initialize control character device */
rv = create_xcdev(xpdev, &xpdev->ctrl_cdev, xdev->config_bar_idx,
NULL, CHAR_CTRL);
if (rv < 0) {
pr_err("create_char(ctrl_cdev) failed\n");
goto fail;
}
xpdev_flag_set(xpdev, XDF_CDEV_CTRL);
/* initialize events character device */
for (i = 0; i < xpdev->user_max; i++) {
rv = create_xcdev(xpdev, &xpdev->events_cdev[i], i, NULL,
CHAR_EVENTS);
if (rv < 0) {
pr_err("create char event %d failed, %d.\n", i, rv);
goto fail;
}
}
xpdev_flag_set(xpdev, XDF_CDEV_EVENT);
/* iterate over channels */
for (i = 0; i < xpdev->h2c_channel_max; i++) {
engine = &xdev->engine_h2c[i];
if (engine->magic != MAGIC_ENGINE)
continue;
rv = create_xcdev(xpdev, &xpdev->sgdma_h2c_cdev[i], i, engine,
CHAR_XDMA_H2C);
if (rv < 0) {
pr_err("create char h2c %d failed, %d.\n", i, rv);
goto fail;
}
}
for (i = 0; i < xpdev->c2h_channel_max; i++) {
engine = &xdev->engine_c2h[i];
if (engine->magic != MAGIC_ENGINE)
continue;
rv = create_xcdev(xpdev, &xpdev->sgdma_c2h_cdev[i], i, engine,
CHAR_XDMA_C2H);
if (rv < 0) {
pr_err("create char c2h %d failed, %d.\n", i, rv);
goto fail;
}
}
xpdev_flag_set(xpdev, XDF_CDEV_SG);
/* Initialize Bypass Character Device */
if (xdev->bypass_bar_idx > 0) {
for (i = 0; i < xpdev->h2c_channel_max; i++) {
engine = &xdev->engine_h2c[i];
if (engine->magic != MAGIC_ENGINE)
continue;
rv = create_xcdev(xpdev, &xpdev->bypass_h2c_cdev[i], i,
engine, CHAR_BYPASS_H2C);
if (rv < 0) {
pr_err("create h2c %d bypass I/F failed, %d.\n",
i, rv);
goto fail;
}
}
for (i = 0; i < xpdev->c2h_channel_max; i++) {
engine = &xdev->engine_c2h[i];
if (engine->magic != MAGIC_ENGINE)
continue;
rv = create_xcdev(xpdev, &xpdev->bypass_c2h_cdev[i], i,
engine, CHAR_BYPASS_C2H);
if (rv < 0) {
pr_err("create c2h %d bypass I/F failed, %d.\n",
i, rv);
goto fail;
}
}
rv = create_xcdev(xpdev, &xpdev->bypass_cdev_base,
xdev->bypass_bar_idx, NULL, CHAR_BYPASS);
if (rv < 0) {
pr_err("create bypass failed %d.\n", rv);
goto fail;
}
xpdev_flag_set(xpdev, XDF_CDEV_BYPASS);
}
/* initialize user character device */
if (xdev->user_bar_idx >= 0) {
rv = create_xcdev(xpdev, &xpdev->user_cdev, xdev->user_bar_idx,
NULL, CHAR_USER);
if (rv < 0) {
pr_err("create_char(user_cdev) failed\n");
goto fail;
}
xpdev_flag_set(xpdev, XDF_CDEV_USER);
/* xvc */
rv = create_xcdev(xpdev, &xpdev->xvc_cdev, xdev->user_bar_idx,
NULL, CHAR_XVC);
if (rv < 0) {
pr_err("create xvc failed, %d.\n", rv);
goto fail;
}
xpdev_flag_set(xpdev, XDF_CDEV_XVC);
}
#ifdef __XDMA_SYSFS__
/* sys file */
rv = device_create_file(&xpdev->pdev->dev,
&dev_attr_xdma_dev_instance);
if (rv) {
pr_err("Failed to create device file\n");
goto fail;
}
#endif
return 0;
fail:
rv = -1;
xpdev_destroy_interfaces(xpdev);
return rv;
}
int xdma_cdev_init(void)
{
g_xdma_class = class_create(THIS_MODULE, XDMA_NODE_NAME);
if (IS_ERR(g_xdma_class)) {
dbg_init(XDMA_NODE_NAME ": failed to create class");
return -EINVAL;
}
return 0;
}
void xdma_cdev_cleanup(void)
{
if (g_xdma_class)
class_destroy(g_xdma_class);
}