-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfops.c
More file actions
284 lines (226 loc) · 6.69 KB
/
Copy pathfops.c
File metadata and controls
284 lines (226 loc) · 6.69 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
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
// SPDX-License-Identifier: <SPDX License Expression>
#include "mx_dma.h"
/******************************************************************************/
/* Functions for file_operations */
/******************************************************************************/
static int mxdma_device_open(struct inode *inode, struct file *file)
{
struct mx_char_dev *mx_cdev;
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;
}
file->private_data = mx_cdev;
return 0;
}
static int mxdma_device_release(struct inode *inode, struct file *file)
{
struct mx_char_dev *mx_cdev;
mx_cdev = (struct mx_char_dev *)file->private_data;
if (!mx_cdev) {
pr_warn("mx_cdev is NULL of file(0x%p)\n", file);
return -EINVAL;
}
if (mx_cdev->magic != MAGIC_CHAR) {
pr_warn("magic is mismatch. mxcdev(0x%p) file(0x%p)\n", mx_cdev, file);
return -EINVAL;
}
file->private_data = 0;
return 0;
}
static int mxdma_device_prepare(struct file *file, struct mx_char_dev **mx_cdev, struct mx_pci_dev **mx_pdev)
{
*mx_cdev = (struct mx_char_dev *)file->private_data;
if (!*mx_cdev) {
pr_warn("mx_cdev is NULL of file(0x%p)\n", file);
return -EINVAL;
}
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 = (*mx_cdev)->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 (!(*mx_pdev)->enabled) {
pr_warn("pci device isn't enabled. dev_no=%d", (*mx_pdev)->dev_no);
return -ENODEV;
}
return 0;
}
static ssize_t mxdma_device_read_data(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
if (!count) {
pr_warn("size of data to read is zero\n");
return -EINVAL;
}
if (pos == NULL || *pos == 0) {
pr_warn("Invalid position to read\n");
return -EINVAL;
}
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
mx_prewake_handlers(mx_pdev);
return read_data_from_device_parallel(mx_pdev, buf, count, pos, IO_OPCODE_DATA_READ);
}
static ssize_t mxdma_device_read_context(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
if (!count) {
pr_warn("size of data to read is zero\n");
return -EINVAL;
}
if (pos == NULL || *pos == 0) {
pr_warn("Invalid position to read\n");
return -EINVAL;
}
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
mx_prewake_handlers(mx_pdev);
return read_data_from_device(mx_pdev, buf, count, pos, IO_OPCODE_CONTEXT_READ);
}
static ssize_t mxdma_device_write_data(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
if (!count) {
pr_warn("size of data to write is zero\n");
return -EINVAL;
}
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
mx_prewake_handlers(mx_pdev);
return write_data_to_device_parallel(mx_pdev, buf, count, pos, IO_OPCODE_DATA_WRITE, false);
}
static ssize_t mxdma_device_write_context(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
if (!count) {
pr_warn("size of data to write is zero\n");
return -EINVAL;
}
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
mx_prewake_handlers(mx_pdev);
return write_data_to_device(mx_pdev, buf, count, pos, IO_OPCODE_CONTEXT_WRITE, false);
}
static long mxdma_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
mx_prewake_handlers(mx_pdev);
return ioctl_to_device(mx_pdev, cmd, arg);
}
static int mxdma_bar_mmap(struct file *file, struct vm_area_struct *vma)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
int ret;
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return ret;
if (!mx_pdev->ops.bar_mmap)
return -EOPNOTSUPP;
return mx_pdev->ops.bar_mmap(mx_pdev, vma);
}
static unsigned int mxdma_device_poll(struct file *file, poll_table *wait)
{
struct mx_char_dev *mx_cdev;
struct mx_pci_dev *mx_pdev;
struct mx_event *mx_event;
int ret;
ret = mxdma_device_prepare(file, &mx_cdev, &mx_pdev);
if (ret)
return POLLERR;
mx_event = &mx_pdev->event;
poll_wait(file, &mx_event->wq, wait);
ret = atomic_read(&mx_event->count);
if (ret > 0) {
atomic_dec(&mx_event->count);
return POLLIN | POLLRDNORM;
}
return 0;
}
static ssize_t mxdma_bdf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct mx_char_dev *mx_cdev = file->private_data;
struct mx_pci_dev *mx_pdev;
struct pci_dev *pdev;
char bdf_str[32];
int len;
if (!mx_cdev || !mx_cdev->mx_pdev)
return -ENODEV;
mx_pdev = mx_cdev->mx_pdev;
pdev = mx_pdev->pdev;
len = scnprintf(bdf_str, sizeof(bdf_str), "%s\n", dev_name(&pdev->dev));
return simple_read_from_buffer(buf, count, ppos, bdf_str, len);
}
static int mxdma_bdf_open(struct inode *inode, struct file *file)
{
struct mx_char_dev *mx_cdev;
mx_cdev = container_of(inode->i_cdev, struct mx_char_dev, cdev);
file->private_data = mx_cdev;
return 0;
}
struct file_operations mxdma_fops_data = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.read = mxdma_device_read_data,
.write = mxdma_device_write_data,
};
struct file_operations mxdma_fops_context = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.read = mxdma_device_read_context,
.write = mxdma_device_write_context,
};
struct file_operations mxdma_fops_ioctl = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.unlocked_ioctl = mxdma_device_ioctl,
.mmap = mxdma_bar_mmap,
};
struct file_operations mxdma_fops_event = {
.owner = THIS_MODULE,
.open = mxdma_device_open,
.release = mxdma_device_release,
.poll = mxdma_device_poll,
};
struct file_operations mxdma_fops_bdf = {
.owner = THIS_MODULE,
.open = mxdma_bdf_open,
.read = mxdma_bdf_read,
};
struct file_operations *mxdma_fops_array[] = {
[MX_CDEV_DATA] = &mxdma_fops_data,
[MX_CDEV_CONTEXT] = &mxdma_fops_context,
[MX_CDEV_IOCTL] = &mxdma_fops_ioctl,
[MX_CDEV_EVENT] = &mxdma_fops_event,
[MX_CDEV_BDF] = &mxdma_fops_bdf,
};