summaryrefslogtreecommitdiffstats
path: root/drivers/staging/greybus/loopback.c
blob: 9914b52c71ceed0c45bb8bf9d2cece41231382ab (plain) (blame)
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
/*
 * Loopback bridge driver for the Greybus loopback module.
 *
 * Copyright 2014 Google Inc.
 * Copyright 2014 Linaro Ltd.
 *
 * Released under the GPLv2 only.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/random.h>
#include <linux/sizes.h>
#include "greybus.h"

struct gb_loopback_stats {
	u32 min;
	u32 max;
	u32 avg;
	u32 sum;
	u32 count;
};

struct gb_loopback {
	struct gb_connection *connection;
	u8 version_major;
	u8 version_minor;

	struct task_struct *task;

	int type;
	u32 size;
	int ms_wait;

	struct gb_loopback_stats latency;
	struct gb_loopback_stats throughput;
	struct gb_loopback_stats frequency;
	struct timeval ts;
	struct timeval te;
	u64 elapsed_nsecs;
	u32 error;
};

/* Version of the Greybus loopback protocol we support */
#define	GB_LOOPBACK_VERSION_MAJOR			0x00
#define	GB_LOOPBACK_VERSION_MINOR			0x01

/* Greybus loopback request types */
#define	GB_LOOPBACK_TYPE_INVALID			0x00
#define	GB_LOOPBACK_TYPE_PROTOCOL_VERSION		0x01
#define	GB_LOOPBACK_TYPE_PING				0x02
#define	GB_LOOPBACK_TYPE_TRANSFER			0x03

#define GB_LOOPBACK_SIZE_MAX				SZ_4K

/* Define get_version() routine */
define_get_version(gb_loopback, LOOPBACK);

/* interface sysfs attributes */
#define gb_loopback_ro_attr(field, type)				\
static ssize_t field##_show(struct device *dev,				\
			    struct device_attribute *attr,		\
			    char *buf)					\
{									\
	struct gb_connection *connection = to_gb_connection(dev);	\
	struct gb_loopback *gb =					\
		(struct gb_loopback *)connection->private;		\
	return sprintf(buf, "%"#type"\n", gb->field);			\
}									\
static DEVICE_ATTR_RO(field)

#define gb_loopback_ro_stats_attr(name, field, type)			\
static ssize_t name##_##field##_show(struct device *dev,		\
			    struct device_attribute *attr,		\
			    char *buf)					\
{									\
	struct gb_connection *connection = to_gb_connection(dev);	\
	struct gb_loopback *gb =					\
		(struct gb_loopback *)connection->private;		\
	return sprintf(buf, "%"#type"\n", gb->name.field);		\
}									\
static DEVICE_ATTR_RO(name##_##field)

#define gb_loopback_stats_attrs(field)					\
	gb_loopback_ro_stats_attr(field, min, d);			\
	gb_loopback_ro_stats_attr(field, max, d);			\
	gb_loopback_ro_stats_attr(field, avg, d);

#define gb_loopback_attr(field, type)					\
static ssize_t field##_show(struct device *dev,				\
			    struct device_attribute *attr,		\
			    char *buf)					\
{									\
	struct gb_connection *connection = to_gb_connection(dev);	\
	struct gb_loopback *gb =					\
		(struct gb_loopback *)connection->private;		\
	return sprintf(buf, "%"#type"\n", gb->field);			\
}									\
static ssize_t field##_store(struct device *dev,			\
			    struct device_attribute *attr,		\
			    const char *buf,				\
			    size_t len)					\
{									\
	int ret;							\
	struct gb_connection *connection = to_gb_connection(dev);	\
	struct gb_loopback *gb =					\
		(struct gb_loopback *)connection->private;		\
	ret = sscanf(buf, "%"#type, &gb->field);			\
	pr_err("%s = %"#type"\n", #field, gb->field);			\
	if (ret != 1)							\
		return -EINVAL;						\
	gb_loopback_check_attr(gb);					\
	return len;							\
}									\
static DEVICE_ATTR_RW(field)

static void gb_loopback_reset_stats(struct gb_loopback *gb);
static void gb_loopback_check_attr(struct gb_loopback *gb)
{
	if (gb->ms_wait > 1000)
		gb->ms_wait = 1000;
	if (gb->type > 3)
		gb->type = 0;
	if (gb->size > GB_LOOPBACK_SIZE_MAX)
		gb->size = GB_LOOPBACK_SIZE_MAX;
	gb->error = 0;
	gb_loopback_reset_stats(gb);
}

/* Time to send and receive one message */
gb_loopback_stats_attrs(latency);
/* Number of packet sent per second on this cport */
gb_loopback_stats_attrs(frequency);
/* Quantity of data sent and received on this cport */
gb_loopback_stats_attrs(throughput);
gb_loopback_ro_attr(error, d);

/*
 * Type of loopback message to send
 * 0 => Don't send message
 * 1 => Send ping message continuously (message without payload)
 * 2 => Send transer message continuously (message with payload)
 */
gb_loopback_attr(type, d);
/* Size of transfer message payload: 0-4096 bytes */
gb_loopback_attr(size, u);
/* Time to wait between two messages: 0-1024 ms */
gb_loopback_attr(ms_wait, d);

#define dev_stats_attrs(name)						\
	&dev_attr_##name##_min.attr,					\
	&dev_attr_##name##_max.attr,					\
	&dev_attr_##name##_avg.attr

static struct attribute *loopback_attrs[] = {
	dev_stats_attrs(latency),
	dev_stats_attrs(frequency),
	dev_stats_attrs(throughput),
	&dev_attr_type.attr,
	&dev_attr_size.attr,
	&dev_attr_ms_wait.attr,
	&dev_attr_error.attr,
	NULL,
};
ATTRIBUTE_GROUPS(loopback);

struct gb_loopback_transfer_request {
	__le32	len;
	__u8	data[0];
};

struct gb_loopback_transfer_response {
	__u8	data[0];
};


static int gb_loopback_transfer(struct gb_loopback *gb,
				struct timeval *tping, u32 len)
{
	struct timeval ts, te;
	u64 elapsed_nsecs;
	struct gb_loopback_transfer_request *request;
	struct gb_loopback_transfer_response *response;
	int retval;

	request = kmalloc(len + sizeof(*request), GFP_KERNEL);
	if (!request)
		return -ENOMEM;
	response = kmalloc(len + sizeof(*response), GFP_KERNEL);
	if (!response) {
		kfree(request);
		return -ENOMEM;
	}

	request->len = cpu_to_le32(len);

	do_gettimeofday(&ts);
	retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_TRANSFER,
				   request, len + sizeof(*request),
				   response, len + sizeof(*response));
	do_gettimeofday(&te);
	elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
	*tping = ns_to_timeval(elapsed_nsecs);

	if (retval)
		goto gb_error;

	if (memcmp(request->data, response->data, len))
		retval = -EREMOTEIO;

gb_error:
	kfree(request);
	kfree(response);

	return retval;
}

static int gb_loopback_ping(struct gb_loopback *gb, struct timeval *tping)
{
	struct timeval ts, te;
	u64 elapsed_nsecs;
	int retval;

	do_gettimeofday(&ts);
	retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_PING,
				   NULL, 0, NULL, 0);
	do_gettimeofday(&te);
	elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
	*tping = ns_to_timeval(elapsed_nsecs);

	return retval;
}

static void gb_loopback_reset_stats(struct gb_loopback *gb)
{
	struct gb_loopback_stats reset = {
		.min = 0xffffffff,
	};
	memcpy(&gb->latency, &reset, sizeof(struct gb_loopback_stats));
	memcpy(&gb->throughput, &reset, sizeof(struct gb_loopback_stats));
	memcpy(&gb->frequency, &reset, sizeof(struct gb_loopback_stats));
	memset(&gb->ts, 0, sizeof(struct timeval));
}

static void gb_loopback_update_stats(struct gb_loopback_stats *stats,
					u64 elapsed_nsecs)
{
	u32 avg;

	if (elapsed_nsecs >= NSEC_PER_SEC) {
		if (!stats->count)
			avg = stats->sum * (elapsed_nsecs / NSEC_PER_SEC);
		else
			avg = stats->sum / stats->count;
		if (stats->min > avg)
			stats->min = avg;
		if (stats->max < avg)
			stats->max = avg;
		stats->avg = avg;
		stats->count = 0;
		stats->sum = 0;
	}
}

static void gb_loopback_freq_update(struct gb_loopback *gb)
{
	gb->frequency.sum++;
	gb_loopback_update_stats(&gb->frequency, gb->elapsed_nsecs);
}

static void gb_loopback_bw_update(struct gb_loopback *gb, int error)
{
	if (!error)
		gb->throughput.sum += gb->size * 2;
	gb_loopback_update_stats(&gb->throughput, gb->elapsed_nsecs);
}

static void gb_loopback_latency_update(struct gb_loopback *gb,
					struct timeval *tlat)
{
	u32 lat;
	u64 nsecs;

	nsecs = timeval_to_ns(tlat);
	lat = nsecs / NSEC_PER_MSEC;

	if (gb->latency.min > lat)
		gb->latency.min = lat;
	if (gb->latency.max < lat)
		gb->latency.max = lat;
	gb->latency.sum += lat;
	gb->latency.count++;
	gb_loopback_update_stats(&gb->latency, gb->elapsed_nsecs);
}

static int gb_loopback_fn(void *data)
{
	int error = 0;
	struct timeval tlat = {0, 0};
	struct gb_loopback *gb = (struct gb_loopback *)data;

	while (!kthread_should_stop()) {
		if (gb->type == 0) {
			msleep(1000);
			continue;
		}
		if (gb->type == 1)
			error = gb_loopback_ping(gb, &tlat);
		if (gb->type == 2)
			error = gb_loopback_transfer(gb, &tlat, gb->size);
		if (error)
			gb->error++;
		if (gb->ts.tv_usec == 0 && gb->ts.tv_sec == 0) {
			do_gettimeofday(&gb->ts);
			continue;
		}
		do_gettimeofday(&gb->te);
		gb->elapsed_nsecs = timeval_to_ns(&gb->te) -
					timeval_to_ns(&gb->ts);
		gb_loopback_freq_update(gb);
		if (gb->type == 2)
			gb_loopback_bw_update(gb, error);
		gb_loopback_latency_update(gb, &tlat);
		if (gb->elapsed_nsecs >= NSEC_PER_SEC)
			gb->ts = gb->te;
		if (gb->ms_wait)
			msleep(gb->ms_wait);

	}
	return 0;
}

static int gb_loopback_connection_init(struct gb_connection *connection)
{
	struct gb_loopback *gb;
	int retval;

	gb = kzalloc(sizeof(*gb), GFP_KERNEL);
	if (!gb)
		return -ENOMEM;

	gb->connection = connection;
	connection->private = gb;
	retval = sysfs_create_groups(&connection->dev.kobj, loopback_groups);
	if (retval)
		goto error;

	/* Check the version */
	retval = get_version(gb);
	if (retval)
		goto error;

	gb_loopback_reset_stats(gb);
	gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback");
	if (IS_ERR(gb->task)) {
		retval = IS_ERR(gb->task);
		goto error;
	}

	return 0;

error:
	kfree(gb);
	return retval;
}

static void gb_loopback_connection_exit(struct gb_connection *connection)
{
	struct gb_loopback *gb = connection->private;

	if (!IS_ERR_OR_NULL(gb->task))
		kthread_stop(gb->task);
	sysfs_remove_groups(&connection->dev.kobj, loopback_groups);
	kfree(gb);
}

static struct gb_protocol loopback_protocol = {
	.name			= "loopback",
	.id			= GREYBUS_PROTOCOL_LOOPBACK,
	.major			= GB_LOOPBACK_VERSION_MAJOR,
	.minor			= GB_LOOPBACK_VERSION_MINOR,
	.connection_init	= gb_loopback_connection_init,
	.connection_exit	= gb_loopback_connection_exit,
	.request_recv		= NULL,	/* no incoming requests */
};

gb_protocol_driver(&loopback_protocol);

MODULE_LICENSE("GPL v2");