-
Notifications
You must be signed in to change notification settings - Fork 3
/
nf10_user.c
473 lines (431 loc) · 13.9 KB
/
nf10_user.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
/*******************************************************************************
*
* NetFPGA-10G http://www.netfpga.org
*
* File:
* nf10_user.c
*
* Project:
*
*
* Author:
* Hwanju Kim
*
* Description:
* This module provides user-level access interface for AXI registers and
* direct access for data path. Note that the current direct access
* by user-level app is done by making buffers permanently to be
* mapped by the app. So, it is a responsibility of the app to copy
* a received buffer to its own user buffer if packet processing takes
* time lagging behind the packet arrival rate. The kernel-user interface
* is minimalistic for now.
*
* This code is initially developed for the Network-as-a-Service (NaaS) project.
* (under development in https://github.com/NetFPGA-NewNIC/linux-driver)
*
*
* Copyright notice:
* Copyright (C) 2014 University of Cambridge
*
* Licence:
* This file is part of the NetFPGA 10G development base package.
*
* This file is free code: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as
* published by the Free Software Foundation.
*
* This package is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the NetFPGA source package. If not, see
* http://www.gnu.org/licenses/.
*
*/
#include "nf10.h"
#include "nf10_user.h"
#include <linux/sched.h>
#include <linux/poll.h>
/* AXI host completion buffer size: 1st 8B for read and 2nd 8B for write */
#define AXI_COMPLETION_SIZE 16
#define AXI_COMPLETION_READ_ADDR 112
#define AXI_COMPLETION_WRITE_ADDR 176
#define AXI_READ_ADDR 64
#define AXI_WRITE_ADDR 128
#define axi_read_completion(adapter) (u64 *)(adapter->axi_completion_kern_addr)
#define axi_write_completion(adapter) (u64 *)(adapter->axi_completion_kern_addr + 0x8)
/* return codes via upper 32bit of completion buffer */
#define AXI_COMPLETION_WAIT 0x0
#define AXI_COMPLETION_OKAY 0x1
#define AXI_COMPLETION_NACK 0x2
#define axi_completion_stat(completion) (u32)(completion >> 32)
#define axi_completion_data(completion) (completion & ((1ULL << 32) - 1))
static dev_t devno;
static struct class *dev_class;
static struct mutex axi_mutex;
DEFINE_SPINLOCK(user_lock);
static int nf10_open(struct inode *n, struct file *f)
{
struct nf10_adapter *adapter = (struct nf10_adapter *)container_of(
n->i_cdev, struct nf10_adapter, cdev);
if (adapter->user_ops == NULL) {
netif_err(adapter, drv, default_netdev(adapter),
"no user_ops is set\n");
return -EINVAL;
}
f->private_data = adapter;
return 0;
}
static int nf10_mmap(struct file *f, struct vm_area_struct *vma)
{
struct nf10_adapter *adapter = f->private_data;
unsigned long pfn;
unsigned long size;
int err = 0;
/* page alignment check */
if ((vma->vm_start & ~PAGE_MASK) || (vma->vm_end & ~PAGE_MASK)) {
netif_err(adapter, drv, default_netdev(adapter),
"not aligned vaddrs (vm_start=%lx vm_end=%lx)",
vma->vm_start, vma->vm_end);
return -EINVAL;
}
/* mmap requires user_ops->get_pfn */
if (!adapter->user_ops->get_pfn)
return -EINVAL;
size = vma->vm_end - vma->vm_start;
if ((pfn = adapter->user_ops->get_pfn(adapter, size)) == 0) {
netif_err(adapter, drv, default_netdev(adapter),
"failed to get pfn (nr_user_mmap=%u)\n",
adapter->nr_user_mmap);
return -EINVAL;
}
/* mmap pfn to the requested user virtual address space */
err = remap_pfn_range(vma, vma->vm_start, pfn, size, vma->vm_page_prot);
netif_dbg(adapter, drv, default_netdev(adapter),
"mmapped [%d] err=%d va=%p pfn=%lx size=%lu\n",
adapter->nr_user_mmap, err, (void *)vma->vm_start, pfn, size);
/* nr_user_mmap is used by user_ops->get_pfn to locate a right kernel
* memory area */
if (!err)
adapter->nr_user_mmap++;
return err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
#define pt_key _key
#else
#define pt_key key
#endif
static unsigned int nf10_poll(struct file *f, poll_table *wait)
{
struct nf10_adapter *adapter = f->private_data;
unsigned int mask = 0;
unsigned long events = wait ? wait->pt_key : POLLIN | POLLOUT | POLLERR;
/* XXX: do we need to immediately return if wait is NULL?
* in old kernel versions, NULL wait is passed when timeout expires,
* but right after following it, a valid wait is passed */
spin_lock_bh(&user_lock);
/* UF_[RX|TX]_PENDING is set by nf10_user_callback from NAPI poll
* handler with IRQ being disabled */
if (events & (POLLIN | POLLRDNORM)) {
/* poll requested for rx */
poll_wait(f, &adapter->user_rx_wq, wait);
if (adapter->user_flags & UF_RX_PENDING) {
adapter->user_flags &= ~UF_RX_PENDING;
mask |= (POLLIN | POLLRDNORM);
}
}
if (events & (POLLOUT | POLLWRNORM)) {
/* poll requested for tx */
poll_wait(f, &adapter->user_tx_wq, wait);
if (adapter->user_flags & UF_TX_PENDING) {
adapter->user_flags &= ~UF_TX_PENDING;
mask |= (POLLOUT | POLLWRNORM);
}
}
/* mask == 0 means it will be sleeping waiting for events,
* so if irq is disabled, enable again before sleeping */
if (!mask && (adapter->user_flags & UF_IRQ_DISABLED)) {
netif_dbg(adapter, intr, default_netdev(adapter),
"enable irq before sleeping (events=%lx)\n", events);
/* if poll is requested for tx, it waits for tx buffer
* availability, so needs to sync user gc address */
if (events & (POLLOUT | POLLWRNORM))
adapter->user_flags |= UF_GC_ADDR_SYNC;
adapter->user_flags &= ~UF_IRQ_DISABLED;
nf10_enable_irq(adapter);
}
spin_unlock_bh(&user_lock);
netif_dbg(adapter, intr, default_netdev(adapter),
"nf10_poll events=%lx mask=%x flags=%x\n",
events, mask, adapter->user_flags);
return mask;
}
/* this threshold is for safety to avoid infinite loop in case AXI interface
* does not respond */
#define AXI_LOOP_THRESHOLD 100000000
static int write_axi(struct nf10_adapter *adapter, u64 addr_val)
{
volatile u64 *completion = axi_write_completion(adapter);
u32 r;
unsigned long loop = 0;
/* init -> write addr & val -> poll stat -> return stat */
*completion = 0;
wmb();
writeq(addr_val, adapter->bar0 + AXI_WRITE_ADDR);
while ((r = axi_completion_stat(*completion)) == AXI_COMPLETION_WAIT) {
if (++loop >= AXI_LOOP_THRESHOLD) {
r = AXI_COMPLETION_NACK;
break;
}
}
netif_dbg(adapter, drv, default_netdev(adapter),
"%s: addr=%llx val=%llx r=%d (loop=%lu)\n",
__func__, addr_val >> 32, addr_val & 0xffffffff, r, loop);
return r;
}
static int read_axi(struct nf10_adapter *adapter, u64 addr, u64 *val)
{
volatile u64 *completion = axi_read_completion(adapter);
u32 r;
unsigned long loop = 0;
/* init -> write addr -> poll stat -> return val & stat */
*completion = 0;
wmb();
writeq(addr, adapter->bar0 + AXI_READ_ADDR);
while ((r = axi_completion_stat(*completion)) == AXI_COMPLETION_WAIT) {
if (++loop >= AXI_LOOP_THRESHOLD) {
r= AXI_COMPLETION_NACK;
break;
}
}
*val = axi_completion_data(*completion);
netif_dbg(adapter, drv, default_netdev(adapter),
"%s: addr=%llx val=%llx r=%d (loop=%lu)\n",
__func__, addr, *val, r, loop);
return r;
}
static int check_axi(int ret)
{
BUG_ON(ret == AXI_COMPLETION_WAIT);
/* let user know returning EFAULT if nacked */
if (ret == AXI_COMPLETION_NACK) {
pr_err("Error: AXI write request gets NACK\n");
return -EFAULT;
}
return 0;
}
static long nf10_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
int ret = 0;
struct nf10_adapter *adapter = (struct nf10_adapter *)f->private_data;
switch(cmd) {
case NF10_IOCTL_CMD_READ_STAT:
/* nothing to do: this placeholder is for compatability
* it was used for debugging purpose of the previous dma */
break;
case NF10_IOCTL_CMD_WRITE_REG:
#ifdef CONFIG_OSNT
case NF10_IOCTL_CMD_WRITE_REG_PY: /* compat w/ OSNT python apps */
#endif
{
/* wraxi */
u32 ret;
u64 addr_val = (u64)arg;
#ifdef CONFIG_OSNT
if (cmd == NF10_IOCTL_CMD_WRITE_REG_PY)
addr_val = *((u64 *)arg);
#endif
mutex_lock(&axi_mutex);
ret = check_axi(write_axi(adapter, addr_val));
mutex_unlock(&axi_mutex);
if (ret)
return ret; /* error */
break;
}
case NF10_IOCTL_CMD_READ_REG:
{
/* rdaxi */
u64 addr, val;
u32 ret;
if (copy_from_user(&addr, (void __user *)arg, 8)) {
pr_err("Error: failed to copy AXI read addr\n");
return -EFAULT;
}
mutex_lock(&axi_mutex);
ret = check_axi(read_axi(adapter, addr, &val));
mutex_unlock(&axi_mutex);
if (ret)
return ret; /* error */
val |= (addr << 32); /* for compatability with older rdaxi */
if (copy_to_user((void __user *)arg, &val, 8)) {
pr_err("Error: failed to copy AXI read val\n");
return -EFAULT;
}
break;
}
case NF10_IOCTL_CMD_INIT:
{
unsigned long ret = 0;
if (adapter->user_flags) {
pr_err("Error: nf10 user stack in use\n");
return -EBUSY;
}
adapter->nr_user_mmap = 0;
adapter->user_flags |= (arg & UF_ON_MASK);
/* when initialized, IRQ is disabled by default, and enabled
* when exiting or before poll() call sleeps */
adapter->user_flags |= UF_IRQ_DISABLED;
nf10_disable_irq(adapter);
if (adapter->user_ops->init) {
ret = adapter->user_ops->init(adapter, arg);
if (copy_to_user((void __user *)arg, &ret, sizeof(u64)))
return -EFAULT;
}
netif_dbg(adapter, drv, default_netdev(adapter),
"user init: flags=%x ret=%lu\n",
adapter->user_flags, ret);
break;
}
case NF10_IOCTL_CMD_EXIT:
{
unsigned long ret = 0;
if (adapter->user_ops->exit) {
ret = adapter->user_ops->exit(adapter, arg);
if (copy_to_user((void __user *)arg, &ret, sizeof(u64)))
return -EFAULT;
}
adapter->nr_user_mmap = 0;
/* IRQ is re-enabled with user gc address synced */
adapter->user_flags = UF_GC_ADDR_SYNC;
nf10_enable_irq(adapter);
netif_dbg(adapter, drv, default_netdev(adapter),
"user exit: flags=%x ret=%lu\n",
adapter->user_flags, ret);
break;
}
case NF10_IOCTL_CMD_PREPARE_RX:
/* arg conveys slot index of rx lbuf */
if (!adapter->user_ops->prepare_rx_buffer)
return -ENOTSUPP;
netif_dbg(adapter, drv, default_netdev(adapter),
"user-driven lbuf preparation: i=%lu\n", arg);
adapter->user_ops->prepare_rx_buffer(adapter, arg);
break;
case NF10_IOCTL_CMD_XMIT:
{
if (!adapter->user_ops->start_xmit)
return -ENOTSUPP;
/* arg conveys reference (index) and size of user tx lbuf */
ret = adapter->user_ops->start_xmit(adapter, arg);
break;
}
default:
return -EINVAL;
}
return ret;
}
static int nf10_release(struct inode *n, struct file *f)
{
f->private_data = NULL;
return 0;
}
static struct file_operations nf10_fops = {
.owner = THIS_MODULE,
.open = nf10_open,
.mmap = nf10_mmap,
.poll = nf10_poll,
.unlocked_ioctl = nf10_ioctl,
.release = nf10_release
};
int nf10_init_fops(struct nf10_adapter *adapter)
{
int err;
/* create /dev/NF10_DRV_NAME char device as user-kernel interface */
if ((err = alloc_chrdev_region(&devno, 0, 1, NF10_DRV_NAME))) {
netif_err(adapter, probe, default_netdev(adapter),
"failed to alloc chrdev\n");
return err;
}
cdev_init(&adapter->cdev, &nf10_fops);
adapter->cdev.owner = THIS_MODULE;
adapter->cdev.ops = &nf10_fops;
if ((err = cdev_add(&adapter->cdev, devno, 1))) {
netif_err(adapter, probe, default_netdev(adapter),
"failed to add cdev\n");
return err;
}
dev_class = class_create(THIS_MODULE, NF10_DRV_NAME);
device_create(dev_class, NULL, devno, NULL, NF10_DRV_NAME);
/* alloc completion buffer for AXI register interface */
adapter->axi_completion_kern_addr = pci_alloc_consistent(adapter->pdev,
AXI_COMPLETION_SIZE, &adapter->axi_completion_dma_addr);
if (adapter->axi_completion_kern_addr == NULL) {
pr_err("Error: failed to alloc axi completion buffer."
" note that axi interface won't work.");
return -ENOMEM;
}
writeq(adapter->axi_completion_dma_addr,
adapter->bar0 + AXI_COMPLETION_READ_ADDR);
writeq(adapter->axi_completion_dma_addr + 0x8,
adapter->bar0 + AXI_COMPLETION_WRITE_ADDR);
mutex_init(&axi_mutex);
return 0;
}
int nf10_remove_fops(struct nf10_adapter *adapter)
{
device_destroy(dev_class, devno);
class_unregister(dev_class);
class_destroy(dev_class);
cdev_del(&adapter->cdev);
unregister_chrdev_region(devno, 1);
if (adapter->axi_completion_kern_addr != NULL)
pci_free_consistent(adapter->pdev, AXI_COMPLETION_SIZE,
adapter->axi_completion_kern_addr,
adapter->axi_completion_dma_addr);
mutex_destroy(&axi_mutex);
return 0;
}
/**
* nf10_user_callback - called by NAPI poll loop when a tx or rx event occurs
* @adapter: associated adapter structure
* @rx: 1 if rx or 0 if tx
*
* Returns true if a relevant process is initialized, false otherwise
**/
bool nf10_user_callback(struct nf10_adapter *adapter, int rx)
{
u32 user_flags;
wait_queue_head_t *this_q, *other_q;
u64 poll_flags;
/* check if user process is initialized for rx or tx */
if ((rx && !(adapter->user_flags & UF_RX_ON)) ||
(!rx && !(adapter->user_flags & UF_TX_ON)))
return false;
/* now we have user process that wants rx or tx */
if (rx) {
user_flags = UF_RX_PENDING;
this_q = &adapter->user_rx_wq;
other_q = &adapter->user_tx_wq;
poll_flags = POLLIN | POLLRDNORM | POLLRDBAND;
}
else { /* tx */
user_flags = UF_TX_PENDING;
this_q = &adapter->user_tx_wq;
other_q = &adapter->user_rx_wq;
poll_flags = POLLOUT | POLLWRNORM | POLLWRBAND;
}
netif_dbg(adapter, drv, default_netdev(adapter),
"try to wake up user process for %s\n", rx ? "RX" : "TX");
spin_lock_bh(&user_lock);
adapter->user_flags |= user_flags;
/* avoid requesting IRQ disabling when any process is waiting in the
* other queue. without this check, the waiting process may never
* wake up. otherwise request NAPI loop to exit with IRQ disabled */
if (!waitqueue_active(other_q))
adapter->user_flags |= UF_IRQ_DISABLED;
wake_up_interruptible_poll(this_q, poll_flags);
spin_unlock_bh(&user_lock);
return true;
}