summaryrefslogtreecommitdiffstats
path: root/driver/sysfs.c
blob: ab0925cb038f899417b89e0eb57c22253d46577f (plain)
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
/**
 *
 * @file sysfs.c
 * @brief This file contains the functions providing the SysFS-interface.
 * @author Guillermo Marcus
 * @date 2010-03-01
 *
 */
#include <linux/version.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/kernel.h>

#include "compat.h"
#include "config.h"
#include "pciDriver.h"
#include "common.h"
#include "umem.h"
#include "kmem.h"
#include "sysfs.h"

static SYSFS_GET_FUNCTION(pcidriver_show_kmem_entry);
static SYSFS_GET_FUNCTION(pcidriver_show_umem_entry);

/**
 *
 * Initializes the sysfs attributes for an kmem/umem-entry
 *
 */
static int _pcidriver_sysfs_initialize(pcidriver_privdata_t *privdata,
					int id,
					struct class_device_attribute *sysfs_attr,
					const char *fmtstring,
					SYSFS_GET_FUNCTION((*callback)))
{
	/* sysfs attributes for kmem buffers don’t make sense before 2.6.13, as
	   we have no mmap support before */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
	char namebuffer[16];

	/* allocate space for the name of the attribute */
	snprintf(namebuffer, sizeof(namebuffer), fmtstring, id);

	if ((sysfs_attr->attr.name = kstrdup(namebuffer, GFP_KERNEL)) == NULL)
		return -ENOMEM;

	sysfs_attr->attr.mode = S_IRUGO;
	sysfs_attr->attr.owner = THIS_MODULE;
	sysfs_attr->show = callback;
	sysfs_attr->store = NULL;
			
	/* name and add attribute */
	if (class_device_create_file(privdata->class_dev, sysfs_attr) != 0)
		return -ENXIO; /* Device not configured. Not the really best choice, but hm. */
#endif

	return 0;
}

int pcidriver_sysfs_initialize_kmem(pcidriver_privdata_t *privdata, int id, struct class_device_attribute *sysfs_attr)
{
	return _pcidriver_sysfs_initialize(privdata, id, sysfs_attr, "kbuf%d", pcidriver_show_kmem_entry);
}

int pcidriver_sysfs_initialize_umem(pcidriver_privdata_t *privdata, int id, struct class_device_attribute *sysfs_attr)
{
	return _pcidriver_sysfs_initialize(privdata, id, sysfs_attr, "umem%d", pcidriver_show_umem_entry);
}

/**
 *
 * Removes the file from sysfs and frees the allocated (kstrdup()) memory.
 *
 */
void pcidriver_sysfs_remove(pcidriver_privdata_t *privdata, struct class_device_attribute *sysfs_attr)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
	class_device_remove_file(privdata->class_dev, sysfs_attr);
	kfree(sysfs_attr->attr.name);
#endif
}

static SYSFS_GET_FUNCTION(pcidriver_show_kmem_entry)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;

        /* As we can be sure that attr.name contains a filename which we
         * created (see _pcidriver_sysfs_initialize), we do not need to have
         * sanity checks but can directly call simple_strtol() */
        int id = simple_strtol(attr->attr.name + strlen("kbuf"), NULL, 10);
	pcidriver_kmem_entry_t *entry = pcidriver_kmem_find_entry_id(privdata, id);
	if (entry)
	    return snprintf(buf, PAGE_SIZE, "buffer: %d\ntype: %lu\nuse: 0x%lx\nitem: %lu\nsize: %lu\nrefs: %lu\nhw ref: %i\nmode: %lx\n", id, entry->type, entry->use, entry->item, entry->size, entry->refs&KMEM_REF_COUNT, (entry->refs&KMEM_REF_HW)?1:0, entry->mode);
	else
	    return snprintf(buf, PAGE_SIZE, "I am in the kmem_entry show function for buffer %d\n", id);
#else
	return 0;
#endif
}

static SYSFS_GET_FUNCTION(pcidriver_show_umem_entry)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
#if 0
	pcidriver_privdata_t *privdata = (pcidriver_privdata_t *)cls->class_data;

	return snprintf(buf, PAGE_SIZE, "I am in the umem_entry show function, class_device_kobj_name: %s\n", cls->kobj.name);
#endif
	return 0;
#else
	return 0;
#endif
}

#ifdef ENABLE_IRQ
SYSFS_GET_FUNCTION(pcidriver_show_irq_count)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;

	return snprintf(buf, PAGE_SIZE, "%d\n", privdata->irq_count);
}

SYSFS_GET_FUNCTION(pcidriver_show_irq_queues)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	int i, offset;

	/* output will be truncated to PAGE_SIZE */
	offset = snprintf(buf, PAGE_SIZE, "Queue\tOutstanding IRQs\n");
	for (i = 0; i < PCIDRIVER_INT_MAXSOURCES; i++)
		offset += snprintf(buf+offset, PAGE_SIZE-offset, "%d\t%d\n", i, atomic_read(&(privdata->irq_outstanding[i])) );

	return (offset > PAGE_SIZE ? PAGE_SIZE : offset+1);
}
#endif

SYSFS_GET_FUNCTION(pcidriver_show_mmap_mode)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;

	return snprintf(buf, PAGE_SIZE, "%d\n", privdata->mmap_mode);
}

SYSFS_SET_FUNCTION(pcidriver_store_mmap_mode)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	int mode = -1;

	/* Set the mmap-mode if it is either PCIDRIVER_MMAP_PCI or PCIDRIVER_MMAP_KMEM */
	if (sscanf(buf, "%d", &mode) == 1 &&
	    (mode == PCIDRIVER_MMAP_PCI || mode == PCIDRIVER_MMAP_KMEM))
		privdata->mmap_mode = mode;

	return strlen(buf);
}

SYSFS_GET_FUNCTION(pcidriver_show_mmap_area)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;

	return snprintf(buf, PAGE_SIZE, "%d\n", privdata->mmap_area);
}

SYSFS_SET_FUNCTION(pcidriver_store_mmap_area)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	int temp = -1;

	sscanf(buf, "%d", &temp);

	if ((temp >= PCIDRIVER_BAR0) && (temp <= PCIDRIVER_BAR5))
		privdata->mmap_area = temp;

	return strlen(buf);
}

SYSFS_GET_FUNCTION(pcidriver_show_kmem_count)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;

	return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&(privdata->kmem_count)));
}

SYSFS_SET_FUNCTION(pcidriver_store_kmem_alloc)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	kmem_handle_t kmem_handle;

	/* FIXME: guillermo: is validation of parsing an unsigned int enough? */
	if (sscanf(buf, "%lu", &kmem_handle.size) == 1)
		pcidriver_kmem_alloc(privdata, &kmem_handle);

	return strlen(buf);
}

SYSFS_SET_FUNCTION(pcidriver_store_kmem_free)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	unsigned int id;
	pcidriver_kmem_entry_t *kmem_entry;

	/* Parse the ID of the kernel memory to be freed, check bounds */
	if (sscanf(buf, "%u", &id) != 1 ||
	    (id >= atomic_read(&(privdata->kmem_count))))
		goto err;

	if ((kmem_entry = pcidriver_kmem_find_entry_id(privdata,id)) == NULL)
		goto err;

	pcidriver_kmem_free_entry(privdata, kmem_entry );
err:
	return strlen(buf);
}

SYSFS_GET_FUNCTION(pcidriver_show_kbuffers)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	int offset = 0;
	struct list_head *ptr;
	pcidriver_kmem_entry_t *entry;

	/* print the header */
	offset += snprintf(buf, PAGE_SIZE, "kbuf#\tcpu addr\tsize\n");

	spin_lock(&(privdata->kmemlist_lock));
	list_for_each(ptr, &(privdata->kmem_list)) {
		entry = list_entry(ptr, pcidriver_kmem_entry_t, list);

		/* print entry info */
		if (offset > PAGE_SIZE) {
			spin_unlock( &(privdata->kmemlist_lock) );
			return PAGE_SIZE;
		}

		offset += snprintf(buf+offset, PAGE_SIZE-offset, "%3d\t%08lx\t%lu\n", entry->id, (unsigned long)(entry->dma_handle), entry->size );
	}

	spin_unlock(&(privdata->kmemlist_lock));

	/* output will be truncated to PAGE_SIZE */
	return (offset > PAGE_SIZE ? PAGE_SIZE : offset+1);
}

SYSFS_GET_FUNCTION(pcidriver_show_umappings)
{
	int offset = 0;
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	struct list_head *ptr;
	pcidriver_umem_entry_t *entry;

	/* print the header */
	offset += snprintf(buf, PAGE_SIZE, "umap#\tn_pages\tsg_ents\n");

	spin_lock( &(privdata->umemlist_lock) );
	list_for_each( ptr, &(privdata->umem_list) ) {
		entry = list_entry(ptr, pcidriver_umem_entry_t, list );

		/* print entry info */
		if (offset > PAGE_SIZE) {
			spin_unlock( &(privdata->umemlist_lock) );
			return PAGE_SIZE;
		}

		offset += snprintf(buf+offset, PAGE_SIZE-offset, "%3d\t%lu\t%lu\n", entry->id,
				(unsigned long)(entry->nr_pages), (unsigned long)(entry->nents));
	}

	spin_unlock( &(privdata->umemlist_lock) );

	/* output will be truncated to PAGE_SIZE */
	return (offset > PAGE_SIZE ? PAGE_SIZE : offset+1);
}

SYSFS_SET_FUNCTION(pcidriver_store_umem_unmap)
{
	pcidriver_privdata_t *privdata = SYSFS_GET_PRIVDATA;
	pcidriver_umem_entry_t *umem_entry;
	unsigned int id;

	if (sscanf(buf, "%u", &id) != 1 ||
	    (id >= atomic_read(&(privdata->umem_count))))
		goto err;

	if ((umem_entry = pcidriver_umem_find_entry_id(privdata, id)) == NULL)
		goto err;

	pcidriver_umem_sgunmap(privdata, umem_entry);
err:
	return strlen(buf);
}