/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
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
/**
 *
 * @file ioctl.c
 * @author Guillermo Marcus
 * @date 2009-04-05
 * @brief Contains the functions handling the different ioctl calls.
 *
 */
#include <linux/version.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/sysfs.h>
#include <asm/atomic.h>
#include <linux/pagemap.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <asm/scatterlist.h>
#include <linux/vmalloc.h>
#include <linux/stat.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/sched.h>

#include "config.h" 			/* Configuration for the driver */
#include "compat.h" 			/* Compatibility functions/definitions */
#include "pciDriver.h" 			/* External interface for the driver */
#include "common.h" 			/* Internal definitions for all parts */
#include "kmem.h" 			/* Internal definitions for kernel memory */
#include "umem.h" 			/* Internal definitions for user space memory */
#include "ioctl.h"			/* Internal definitions for the ioctl part */

/** Declares a variable of the given type with the given name and copies it from userspace */
#define READ_FROM_USER(type, name) \
	type name; \
	if ((ret = copy_from_user(&name, (type*)arg, sizeof(name))) != 0) \
		return -EFAULT;

/** Writes back the given variable with the given type to userspace */
#define WRITE_TO_USER(type, name) \
	if ((ret = copy_to_user((type*)arg, &name, sizeof(name))) != 0) \
		return -EFAULT;

/**
 *
 * Sets the mmap mode for following mmap() calls.
 *
 * @param arg Not a pointer, but either PCIDRIVER_MMAP_PCI or PCIDRIVER_MMAP_KMEM
 *
 */
static int ioctl_mmap_mode(pcidriver_privdata_t *privdata, unsigned long arg)
{
	if ((arg != PCIDRIVER_MMAP_PCI) && (arg != PCIDRIVER_MMAP_KMEM))
		return -EINVAL;

	/* change the mode */
	privdata->mmap_mode = arg;

	return 0;
}

/**
 *
 * Sets the mmap area (BAR) for following mmap() calls.
 *
 */
static int ioctl_mmap_area(pcidriver_privdata_t *privdata, unsigned long arg)
{
	/* validate input */
	if ((arg < PCIDRIVER_BAR0) || (arg > PCIDRIVER_BAR5))
		return -EINVAL;

	/* change the PCI area to mmap */
	privdata->mmap_area = arg;

	return 0;
}

/**
 *
 * Reads/writes a byte/word/dword of the device's PCI config.
 *
 * @see pcidriver_pci_read
 * @see pcidriver_pci_write
 *
 */
static int ioctl_pci_config_read_write(pcidriver_privdata_t *privdata, unsigned int cmd, unsigned long arg)
{
	int ret;
	READ_FROM_USER(pci_cfg_cmd, pci_cmd);

	if (cmd == PCIDRIVER_IOC_PCI_CFG_RD) {
		switch (pci_cmd.size) {
			case PCIDRIVER_PCI_CFG_SZ_BYTE:
				ret = pci_read_config_byte( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.byte) );
				break;
			case PCIDRIVER_PCI_CFG_SZ_WORD:
				ret = pci_read_config_word( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.word) );
				break;
			case PCIDRIVER_PCI_CFG_SZ_DWORD:
				ret = pci_read_config_dword( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.dword) );
				break;
			default:
				return -EINVAL;		/* Wrong size setting */
		}
	} else {
		switch (pci_cmd.size) {
			case PCIDRIVER_PCI_CFG_SZ_BYTE:
				ret = pci_write_config_byte( privdata->pdev, pci_cmd.addr, pci_cmd.val.byte );
				break;
			case PCIDRIVER_PCI_CFG_SZ_WORD:
				ret = pci_write_config_word( privdata->pdev, pci_cmd.addr, pci_cmd.val.word );
				break;
			case PCIDRIVER_PCI_CFG_SZ_DWORD:
				ret = pci_write_config_dword( privdata->pdev, pci_cmd.addr, pci_cmd.val.dword );
				break;
			default:
				return -EINVAL;		/* Wrong size setting */
				break;
		}
	}

	WRITE_TO_USER(pci_cfg_cmd, pci_cmd);

	return 0;
}

/**
 *
 * Gets the PCI information for the device.
 *
 * @see pcidriver_pci_info
 *
 */
static int ioctl_pci_info(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	int bar;
	READ_FROM_USER(pci_board_info, pci_info);

	pci_info.vendor_id = privdata->pdev->vendor;
	pci_info.device_id = privdata->pdev->device;
	pci_info.bus = privdata->pdev->bus->number;
	pci_info.slot = PCI_SLOT(privdata->pdev->devfn);
	pci_info.devfn = privdata->pdev->devfn;

	if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_PIN, &(pci_info.interrupt_pin))) != 0)
		return ret;

	if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_LINE, &(pci_info.interrupt_line))) != 0)
		return ret;

	for (bar = 0; bar < 6; bar++) {
		pci_info.bar_start[bar] = pci_resource_start(privdata->pdev, bar);
		pci_info.bar_length[bar] = pci_resource_len(privdata->pdev, bar);
	}

	WRITE_TO_USER(pci_board_info, pci_info);

	return 0;
}

/**
 *
 * Allocates kernel memory.
 *
 * @see pcidriver_kmem_alloc
 *
 */
static int ioctl_kmem_alloc(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(kmem_handle_t, khandle);

	if ((ret = pcidriver_kmem_alloc(privdata, &khandle)) != 0)
		return ret;

	WRITE_TO_USER(kmem_handle_t, khandle);

	return 0;
}

/**
 *
 * Frees kernel memory.
 *
 * @see pcidriver_kmem_free
 *
 */
static int ioctl_kmem_free(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(kmem_handle_t, khandle);

	if ((ret = pcidriver_kmem_free(privdata, &khandle)) != 0)
		return ret;

	return 0;
}

/**
 *
 * Syncs kernel memory.
 *
 * @see pcidriver_kmem_sync
 *
 */
static int ioctl_kmem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(kmem_sync_t, ksync);

	return pcidriver_kmem_sync(privdata, &ksync);
}

/*
 *
 * Maps the given scatter/gather list from memory to PCI bus addresses.
 *
 * @see pcidriver_umem_sgmap
 *
 */
static int ioctl_umem_sgmap(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(umem_handle_t, uhandle);

	if ((ret = pcidriver_umem_sgmap(privdata, &uhandle)) != 0)
		return ret;

	WRITE_TO_USER(umem_handle_t, uhandle);

	return 0;
}

/**
 *
 * Unmaps the given scatter/gather list.
 *
 * @see pcidriver_umem_sgunmap
 *
 */
static int ioctl_umem_sgunmap(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	pcidriver_umem_entry_t *umem_entry;
	READ_FROM_USER(umem_handle_t, uhandle);

	/* Find the associated umem_entry for this buffer,
	 * return -EINVAL if the specified handle id is invalid */
	if ((umem_entry = pcidriver_umem_find_entry_id(privdata, uhandle.handle_id)) == NULL)
		return -EINVAL;

	if ((ret = pcidriver_umem_sgunmap(privdata, umem_entry)) != 0)
		return ret;

	return 0;
}

/**
 *
 * Copies the scatter/gather list from kernelspace to userspace.
 *
 * @see pcidriver_umem_sgget
 *
 */
static int ioctl_umem_sgget(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(umem_sglist_t, usglist);

	/* The umem_sglist_t has a pointer to the scatter/gather list itself which
	 * needs to be copied separately. The number of elements is stored in ->nents.
	 * As the list can get very big, we need to use vmalloc. */
	if ((usglist.sg = vmalloc(usglist.nents * sizeof(umem_sgentry_t))) == NULL)
		return -ENOMEM;

	/* copy array to kernel structure */
	ret = copy_from_user(usglist.sg, ((umem_sglist_t *)arg)->sg, (usglist.nents)*sizeof(umem_sgentry_t));
	if (ret) return -EFAULT;

	if ((ret = pcidriver_umem_sgget(privdata, &usglist)) != 0)
		return ret;

	/* write data to user space */
	ret = copy_to_user(((umem_sglist_t *)arg)->sg, usglist.sg, (usglist.nents)*sizeof(umem_sgentry_t));
	if (ret) return -EFAULT;

	/* free array memory */
	vfree(usglist.sg);

	/* restore sg pointer to vma address in user space before copying */
	usglist.sg = ((umem_sglist_t *)arg)->sg;

	WRITE_TO_USER(umem_sglist_t, usglist);

	return 0;
}

/**
 *
 * Syncs user memory.
 *
 * @see pcidriver_umem_sync
 *
 */
static int ioctl_umem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
{
	int ret;
	READ_FROM_USER(umem_handle_t, uhandle);

	return pcidriver_umem_sync( privdata, &uhandle );
}

/**
 *
 * Waits for an interrupt
 *
 * @param arg Not a pointer, but the irq source to wait for (unsigned int)
 *
 */
static int ioctl_wait_interrupt(pcidriver_privdata_t *privdata, unsigned long arg)
{
#ifdef ENABLE_IRQ
	unsigned int irq_source;
	int temp;

	if (arg >= PCIDRIVER_INT_MAXSOURCES)
		return -EFAULT;						/* User tried to overrun the IRQ_SOURCES array */

	irq_source = arg;

	/* Thanks to Joern for the correction and tips! */
	/* done this way to avoid wrong behaviour (endless loop) of the compiler in AMD platforms */
	temp=1;
	while (temp) {
		/* We wait here with an interruptible timeout. This will be interrupted
                 * by int.c:check_acknowledge_channel() as soon as in interrupt for
                 * the specified source arrives. */
		wait_event_interruptible_timeout( (privdata->irq_queues[irq_source]), (atomic_read(&(privdata->irq_outstanding[irq_source])) > 0), (10*HZ/1000) );

		if (atomic_add_negative( -1, &(privdata->irq_outstanding[irq_source])) )
			atomic_inc( &(privdata->irq_outstanding[irq_source]) );
		else
			temp =0;
	}

	return 0;
#else
	mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
	return -EFAULT;
#endif
}

/**
 *
 * Clears the interrupt wait queue.
 *
 * @param arg Not a pointer, but the irq source (unsigned int)
 * @returns -EFAULT if the user specified an irq source out of range
 *
 */
static int ioctl_clear_ioq(pcidriver_privdata_t *privdata, unsigned long arg)
{
#ifdef ENABLE_IRQ
	unsigned int irq_source;

	if (arg >= PCIDRIVER_INT_MAXSOURCES)
		return -EFAULT;

	irq_source = arg;
	atomic_set(&(privdata->irq_outstanding[irq_source]), 0);

	return 0;
#else
	mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
	return -EFAULT;
#endif
}

/**
 *
 * This function handles all ioctl file operations.
 * Generally, the data of the ioctl is copied from userspace to kernelspace, a separate
 * function is called to handle the ioctl itself, then the data is copied back to userspace.
 *
 * @returns -EFAULT when an invalid memory pointer is passed
 *
 */
int pcidriver_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
	pcidriver_privdata_t *privdata = filp->private_data;

	/* Select the appropiate command */
	switch (cmd) {
		case PCIDRIVER_IOC_MMAP_MODE:
			return ioctl_mmap_mode(privdata, arg);

		case PCIDRIVER_IOC_MMAP_AREA:
			return ioctl_mmap_area(privdata, arg);

		case PCIDRIVER_IOC_PCI_CFG_RD:
		case PCIDRIVER_IOC_PCI_CFG_WR:
			return ioctl_pci_config_read_write(privdata, cmd, arg);

		case PCIDRIVER_IOC_PCI_INFO:
			return ioctl_pci_info(privdata, arg);

		case PCIDRIVER_IOC_KMEM_ALLOC:
			return ioctl_kmem_alloc(privdata, arg);

		case PCIDRIVER_IOC_KMEM_FREE:
			return ioctl_kmem_free(privdata, arg);

		case PCIDRIVER_IOC_KMEM_SYNC:
			return ioctl_kmem_sync(privdata, arg);

		case PCIDRIVER_IOC_UMEM_SGMAP:
			return ioctl_umem_sgmap(privdata, arg);

		case PCIDRIVER_IOC_UMEM_SGUNMAP:
			return ioctl_umem_sgunmap(privdata, arg);

		case PCIDRIVER_IOC_UMEM_SGGET:
			return ioctl_umem_sgget(privdata, arg);

		case PCIDRIVER_IOC_UMEM_SYNC:
			return ioctl_umem_sync(privdata, arg);

		case PCIDRIVER_IOC_WAITI:
			return ioctl_wait_interrupt(privdata, arg);

		case PCIDRIVER_IOC_CLEAR_IOQ:
			return ioctl_clear_ioq(privdata, arg);

		default:
			return -EINVAL;
	}
}