forked from MicroTCA/upciedev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pciedev_ioctl_exp.c
335 lines (310 loc) · 11.2 KB
/
pciedev_ioctl_exp.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
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include "pciedev_ufn.h"
#include "pciedev_io.h"
#include "read_write_inline.h"
long pciedev_ioctl_exp(struct file *filp, unsigned int *cmd_p, unsigned long *arg_p, pciedev_cdev * pciedev_cdev_m)
{
unsigned int cmd;
unsigned long arg;
pid_t cur_proc = 0;
int minor = 0;
int d_num = 0;
int retval = 0;
int err = 0;
int nUserValue;
struct pci_dev* pdev;
u_int tmp_offset;
u_int tmp_data;
u_int tmp_cmd;
u_int tmp_reserved;
int io_size;
void *address ;
u8 tmp_data_8;
u16 tmp_data_16;
u32 tmp_data_32;
device_ioc_rw aIocRW;
device_vector_rw aVectorRW;
device_ioc_rw* __user pUserRWdata;
int i, nMaxNumRW;
const size_t unSizeOfRW = sizeof(device_ioc_rw);
device_ioctrl_data data;
struct pciedev_dev *dev = filp->private_data;
cmd = *cmd_p;
arg = *arg_p;
io_size = sizeof(device_ioctrl_data);
minor = dev->dev_minor;
d_num = dev->dev_num;
cur_proc = current->group_leader->pid;
pdev = (dev->pciedev_pci_dev);
if(!dev->dev_sts){
printk("PCIEDEV_IOCTRL: NO DEVICE %d\n", dev->dev_num);
retval = -EFAULT;
return retval;
}
DEBUGNEW("\n");
/*
* the direction is a bitmask, and VERIFY_WRITE catches R/W
* transfers. `Type' is user-oriented, while
* access_ok is kernel-oriented, so the concept of "read" and
* "write" is reversed
*/
if (_IOC_DIR(cmd) & _IOC_WRITE)
{
err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
if (err){ ERRCT("Bad pointer!\n"); }
}
else if (_IOC_DIR(cmd) & _IOC_READ)
{
err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
if (err){ ERRCT("Bad pointer!\n"); }
}
if (err) return -EFAULT;
//if (mutex_lock_interruptible(&dev->dev_mut))return -ERESTARTSYS;
//if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; } /// new // removed to the cases
switch (cmd) {
case PCIEDEV_PHYSICAL_SLOT:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
retval = 0;
if (copy_from_user(&data, (device_ioctrl_data*)arg, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
tmp_offset = data.offset;
tmp_data = data.data;
tmp_cmd = data.cmd;
tmp_reserved = data.reserved;
data.data = dev->slot_num;
data.cmd = PCIEDEV_PHYSICAL_SLOT;
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_DRIVER_VERSION:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
data.data = pciedev_cdev_m->PCIEDEV_DRV_VER_MAJ;
data.offset = pciedev_cdev_m->PCIEDEV_DRV_VER_MIN;
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_UDRIVER_VERSION:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
data.data = pciedev_cdev_m->UPCIEDEV_VER_MAJ;
data.offset = pciedev_cdev_m->UPCIEDEV_VER_MIN;
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_FIRMWARE_VERSION:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
data.data = dev->brd_info_list.PCIEDEV_BOARD_VERSION;
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_GET_STATUS:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
data.data = dev->dev_sts;
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_SCRATCH_REG:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
retval = 0;
if (copy_from_user(&data, (device_ioctrl_data*)arg, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
tmp_offset = data.offset;
tmp_data = data.data;
tmp_cmd = data.cmd;
if(tmp_cmd == 1){
data.data = dev->scratch_bar;
data.offset = dev->scratch_offset;
}
if(tmp_cmd == 0){
dev->scratch_bar = data.data;
dev->scratch_offset = data.offset;
}
if(tmp_cmd == 2){
address = pciedev_get_baraddress(dev->scratch_bar, dev);
if (!address){
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
switch(dev->register_size){
case RW_D8:
tmp_data_8 = ioread8(address + dev->scratch_offset);
data.data = tmp_data_8;
break;
case RW_D16:
tmp_data_16 = ioread16(address + dev->scratch_offset);
data.data = tmp_data_16;
break;
case RW_D32:
tmp_data_32 = ioread32(address + dev->scratch_offset);
data.data = tmp_data_32;
break;
}
}
if (copy_to_user((device_ioctrl_data*)arg, &data, (size_t)io_size)) {
retval = -EFAULT;
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut);
return retval;
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_SET_SWAP:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
if (copy_from_user(&nUserValue, (int*)arg, sizeof(int))) {
//mutex_unlock(&dev->dev_mut);
LeaveCritRegion(&dev->dev_mut); /// new
return -EFAULT;
}
dev->swap = nUserValue ? 1 : 0;
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_GET_SWAP:
return dev->swap;
case PCIEDEV_LOCK_DEVICE:
retval = LongLockOfCriticalRegion(&dev->dev_mut);
break;
case PCIEDEV_UNLOCK_DEVICE:
retval = UnlockLongCritRegion(&dev->dev_mut);
break;
case PCIEDEV_SET_BITS:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
if (copy_from_user(&aIocRW, (device_ioc_rw*)arg, sizeof(device_ioc_rw))){
LeaveCritRegion(&dev->dev_mut);
return -EFAULT;
}
//aIocRW.mode_rw = W_BITS_INITIAL + (aIocRW.mode_rw&_STEP_FOR_NEXT_MODE2_);
CORRECT_REGISTER_SIZE(aIocRW.register_size, dev->register_size);
retval = pciedev_write_inline(dev, aIocRW.register_size, MTCA_SET_BITS, aIocRW.barx_rw, aIocRW.offset_rw,
(const char*)aIocRW.dataPtr, (const char*)aIocRW.maskPtr, aIocRW.count_rw);
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_SWAP_BITS:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
if (copy_from_user(&aIocRW, (device_ioc_rw*)arg, sizeof(device_ioc_rw))){
LeaveCritRegion(&dev->dev_mut);
return -EFAULT;
}
CORRECT_REGISTER_SIZE(aIocRW.register_size, dev->register_size);
//ALERTCT("!!!!!!!!!!!!!!!! mode=%s\n", ModeString(aIocRW.register_size));
retval = pciedev_write_inline(dev, aIocRW.register_size, MTCA_SWAP_BITS,aIocRW.barx_rw, aIocRW.offset_rw,
NULL, (const char*)aIocRW.maskPtr, aIocRW.count_rw);
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_LOCKED_READ:
if (copy_from_user(&aIocRW, (device_ioc_rw*)arg, unSizeOfRW)){ return -EFAULT; }
CORRECT_REGISTER_SIZE(aIocRW.register_size, dev->register_size);
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
retval = pciedev_read_inline(dev, aIocRW.register_size, MTCA_LOCKED_READ,aIocRW.barx_rw,
aIocRW.offset_rw, (char*)aIocRW.dataPtr, aIocRW.count_rw);
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_VECTOR_RW:
if (copy_from_user(&aVectorRW, (device_vector_rw*)arg, sizeof(device_vector_rw))){return -EFAULT;}
nMaxNumRW = (int)aVectorRW.number_of_rw;
pUserRWdata = (device_ioc_rw* __user)((void*)aVectorRW.device_ioc_rw_ptr);
retval = 0;
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
//ALERTCT("!!!!!!!!!!!!!!!! VECTOR READ NUM_READS %i\n", nMaxNumRW);
/*
for (i = 0; (i < nMaxNumRW) && (!copy_from_user(&aIocRW, pUserRWdata, unSizeOfRW)); ++i,
pUserRWdata += sizeof(device_ioc_rw))
*/
for (i = 0; (i < nMaxNumRW) && (!copy_from_user(&aIocRW, pUserRWdata, unSizeOfRW)); ++i,
pUserRWdata += 1)
{
CORRECT_REGISTER_SIZE(aIocRW.register_size, dev->register_size);
if (aIocRW.rw_access_mode<MTCA_SIMPLE_WRITE)
{
//ALERTCT("!!!!!! VECTOR READ NUM %i, REG_SIZE %i BAR%i COUNT %i\n",
//i, aIocRW.register_size, aIocRW.barx_rw, aIocRW.count_rw);
retval += pciedev_read_inline(dev, aIocRW.register_size, aIocRW.rw_access_mode,
aIocRW.barx_rw,aIocRW.offset_rw, (char*)aIocRW.dataPtr, aIocRW.count_rw);
}
else
{
retval += pciedev_write_inline(dev, aIocRW.register_size, aIocRW.rw_access_mode, aIocRW.barx_rw,
aIocRW.offset_rw,(const char*)aIocRW.dataPtr, (const char*)aIocRW.maskPtr, aIocRW.count_rw);
}
}
LeaveCritRegion(&dev->dev_mut);
break;
case PCIEDEV_GET_REGISTER_SIZE:
DEBUGNEW("!!!PCIEDEV_GET_REGISTER_SIZE reg_size=%d\n", (int)dev->register_size);
return dev->register_size;
break;
case PCIEDEV_SET_REGISTER_SIZE:
if (copy_from_user(&nUserValue, (int*)arg, sizeof(int))) {
return -EFAULT;
}
CORRECT_REGISTER_SIZE(nUserValue, RW_D32);
dev->register_size = nUserValue;
break;
case PCIEDEV_SINGLE_IOC_ACCESS:
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
if (copy_from_user(&aIocRW, (device_ioc_rw*)arg, sizeof(device_ioc_rw)))
{
LeaveCritRegion(&dev->dev_mut);
return -EFAULT;
}
CORRECT_REGISTER_SIZE(aIocRW.register_size, dev->register_size);
if (aIocRW.rw_access_mode<MTCA_SIMPLE_WRITE)
{
if (aIocRW.rw_access_mode == MTCA_LOCKED_READ){ if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; } }
retval += pciedev_read_inline(dev, aIocRW.register_size, aIocRW.rw_access_mode,
aIocRW.barx_rw, aIocRW.offset_rw, (char*)aIocRW.dataPtr, aIocRW.count_rw);
if (aIocRW.rw_access_mode == MTCA_LOCKED_READ){ LeaveCritRegion(&dev->dev_mut); }
}
else
{
if (EnterCritRegion(&dev->dev_mut)){ return -ERESTARTSYS; }
retval += pciedev_write_inline(dev, aIocRW.register_size, aIocRW.rw_access_mode, aIocRW.barx_rw,
aIocRW.offset_rw, (const char*)aIocRW.dataPtr, (const char*)aIocRW.maskPtr, aIocRW.count_rw);
LeaveCritRegion(&dev->dev_mut);
}
break;
default:
return -ENOTTY;
break;
}
//mutex_unlock(&dev->dev_mut);
//LeaveCritRegion(&dev->dev_mut); /// new // removed to the cases
return retval;
}
EXPORT_SYMBOL(pciedev_ioctl_exp);