Repository navigation
Expand file tree
/
Copy pathfops.c
More file actions
221 lines (189 loc) · 5.37 KB
/
Copy pathfops.c
File metadata and controls
221 lines (189 loc) · 5.37 KB
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
// SPDX-License-Identifier: <SPDX License Expression>
#include "mx_dma.h"
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif
static int mxdma_device_open(struct inode *inode, struct file *file)
{
struct mx_char_dev *mx_cdev;
struct mx_file_ctx *ctx;
mx_cdev = container_of(inode->i_cdev, struct mx_char_dev, cdev);
if (mx_cdev->magic != MAGIC_CHAR) {
pr_warn("magic is mismatch. mxcdev(0x%p) inode(%#lx)\n", mx_cdev, inode->i_ino);
return -EINVAL;
}
if (percpu_ref_is_dying(&mx_cdev->mx_pdev->io_ref) || !mx_pdev_get_live(mx_cdev->mx_pdev))
return -ENODEV;
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx) {
mx_pdev_put(mx_cdev->mx_pdev);
return -ENOMEM;
}
ctx->magic = MAGIC_FILE_CTX;
refcount_set(&ctx->refs, 1);
ctx->mx_cdev = mx_cdev;
ctx->mx_pdev = mx_cdev->mx_pdev;
file->private_data = ctx;
return 0;
}
static int mxdma_device_release(struct inode *inode, struct file *file)
{
struct mx_file_ctx *ctx = file->private_data;
if (!ctx || ctx->magic != MAGIC_FILE_CTX) {
pr_warn("invalid private context of file(0x%p)\n", file);
return -EINVAL;
}
file->private_data = NULL;
mx_file_ctx_put(ctx);
return 0;
}
static int mxdma_device_prepare(struct file *file, struct mx_file_ctx **ctx,
struct mx_char_dev **mx_cdev,
struct mx_pci_dev **mx_pdev)
{
*ctx = file->private_data;
if (!*ctx || (*ctx)->magic != MAGIC_FILE_CTX) {
pr_warn("invalid private context of file(0x%p)\n", file);
return -EINVAL;
}
*mx_cdev = (*ctx)->mx_cdev;
if ((*mx_cdev)->magic != MAGIC_CHAR) {
pr_warn("magic is mismatch. mxcdev(0x%p) file(0x%p)\n", *mx_cdev, file);
return -EINVAL;
}
*mx_pdev = (*ctx)->mx_pdev;
if (!*mx_pdev) {
pr_warn("mx_pdev is NULL of file(0x%p)\n", file);
return -EINVAL;
}
if ((*mx_pdev)->magic != MAGIC_DEVICE) {
pr_warn("magic is mismatch. mx_pdev(0x%p) file(0x%p)\n", *mx_pdev, file);
return -EINVAL;
}
if (!percpu_ref_tryget_live(&(*mx_pdev)->io_ref))
return -ENODEV;
if (READ_ONCE((*mx_pdev)->lease.removed) || !(*mx_pdev)->enabled) {
percpu_ref_put(&(*mx_pdev)->io_ref);
return -ENODEV;
}
return 0;
}
static void mxdma_device_finish(struct mx_pci_dev *mx_pdev)
{
percpu_ref_put(&mx_pdev->io_ref);
}
static long mxdma_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct mx_file_ctx *ctx;
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
long ret;
ret = mxdma_device_prepare(file, &ctx, &mx_cdev, &mx_pdev);
if (ret)
return ret;
if (mx_lease_ioctl_cmd(cmd)) {
ret = mx_lease_ioctl(ctx, cmd, arg);
} else if (!mx_ioctl_is_direct_cmd(cmd)) {
ret = ioctl_to_device(ctx, cmd, arg);
} else {
ret = mx_lease_direct_begin(ctx);
if (!ret) {
mx_prewake_handlers(mx_pdev);
ret = ioctl_to_device(ctx, cmd, arg);
mx_lease_direct_end(ctx);
}
}
mxdma_device_finish(mx_pdev);
return ret;
}
#ifdef CONFIG_COMPAT
static long mxdma_device_compat_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
if (!mx_lease_ioctl_cmd(cmd))
return -ENOIOCTLCMD;
return mxdma_device_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
#endif
static int mxdma_bar_mmap(struct file *file, struct vm_area_struct *vma)
{
struct mx_file_ctx *ctx;
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
ret = mxdma_device_prepare(file, &ctx, &mx_cdev, &mx_pdev);
if (ret)
return ret;
ret = mx_lease_direct_begin(ctx);
if (!ret) {
ret = mx_bar_mmap(mx_pdev, ctx, vma);
mx_lease_direct_end(ctx);
}
mxdma_device_finish(mx_pdev);
return ret;
}
static __poll_t mxdma_device_poll(struct file *file, poll_table *wait)
{
struct mx_file_ctx *ctx;
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
struct mx_event *mx_event;
__poll_t mask = 0;
int ret;
ret = mxdma_device_prepare(file, &ctx, &mx_cdev, &mx_pdev);
if (ret)
return ret == -ENODEV ? EPOLLERR | EPOLLHUP : EPOLLERR;
mx_event = &mx_pdev->event;
poll_wait(file, &mx_event->wq, wait);
if (percpu_ref_is_dying(&mx_pdev->io_ref) || READ_ONCE(mx_pdev->lease.removed))
mask = EPOLLERR | EPOLLHUP;
else if (atomic_read(&mx_event->count) > 0) {
atomic_dec(&mx_event->count);
mask = EPOLLIN | EPOLLRDNORM;
}
mxdma_device_finish(mx_pdev);
return mask;
}
static ssize_t mxdma_bdf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct mx_file_ctx *ctx;
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
char bdf_str[32];
ssize_t ret;
int len;
ret = mxdma_device_prepare(file, &ctx, &mx_cdev, &mx_pdev);
if (ret)
return ret;
len = scnprintf(bdf_str, sizeof(bdf_str), "%s\n", mx_pdev->bdf);
ret = simple_read_from_buffer(buf, count, ppos, bdf_str, len);
mxdma_device_finish(mx_pdev);
return ret;
}
static const struct file_operations mxdma_fops_ioctl = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.unlocked_ioctl = mxdma_device_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = mxdma_device_compat_ioctl,
#endif
.mmap = mxdma_bar_mmap,
};
static const struct file_operations mxdma_fops_event = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.poll = mxdma_device_poll,
};
static const struct file_operations mxdma_fops_bdf = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.read = mxdma_bdf_read,
};
const struct file_operations *mxdma_fops_array[] = {
[MX_CDEV_IOCTL] = &mxdma_fops_ioctl,
[MX_CDEV_EVENT] = &mxdma_fops_event,
[MX_CDEV_BDF] = &mxdma_fops_bdf,
};