2015-03-31 07:51:59 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2015-07-28 17:34:36 +00:00
|
|
|
#include <linux/mutex.h>
|
2015-03-31 07:51:59 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/random.h>
|
2015-03-31 21:01:45 +00:00
|
|
|
#include <linux/sizes.h>
|
2015-07-28 17:34:38 +00:00
|
|
|
#include <linux/cdev.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/kfifo.h>
|
|
|
|
|
2015-04-16 07:53:59 +00:00
|
|
|
#include <asm/div64.h>
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
#include "greybus.h"
|
|
|
|
|
|
|
|
struct gb_loopback_stats {
|
2015-08-03 17:57:14 +00:00
|
|
|
u32 min;
|
|
|
|
u32 max;
|
2015-07-21 22:50:08 +00:00
|
|
|
u64 avg;
|
|
|
|
u64 sum;
|
|
|
|
u64 count;
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_loopback {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
u8 version_major;
|
|
|
|
u8 version_minor;
|
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
struct kfifo kfifo;
|
2015-07-28 17:34:36 +00:00
|
|
|
struct mutex mutex;
|
2015-03-31 07:51:59 +00:00
|
|
|
struct task_struct *task;
|
2015-07-24 09:02:56 +00:00
|
|
|
wait_queue_head_t wq;
|
2015-07-28 17:34:38 +00:00
|
|
|
dev_t dev;
|
|
|
|
struct cdev cdev;
|
|
|
|
struct device *device;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
int type;
|
|
|
|
u32 size;
|
2015-07-21 22:50:08 +00:00
|
|
|
u32 iteration_max;
|
|
|
|
u32 iteration_count;
|
2015-07-13 23:53:13 +00:00
|
|
|
size_t size_max;
|
2015-03-31 07:51:59 +00:00
|
|
|
int ms_wait;
|
|
|
|
|
|
|
|
struct gb_loopback_stats latency;
|
|
|
|
struct gb_loopback_stats throughput;
|
2015-07-21 22:50:10 +00:00
|
|
|
struct gb_loopback_stats requests_per_second;
|
2015-03-31 07:51:59 +00:00
|
|
|
u64 elapsed_nsecs;
|
|
|
|
u32 error;
|
|
|
|
};
|
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
#define GB_LOOPBACK_FIFO_DEFAULT 8192
|
|
|
|
|
|
|
|
static struct class *loopback_class;
|
|
|
|
static int loopback_major;
|
|
|
|
static const struct file_operations loopback_fops;
|
|
|
|
static DEFINE_IDA(minors);
|
|
|
|
static unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT;
|
|
|
|
module_param(kfifo_depth, uint, 0444);
|
|
|
|
|
|
|
|
/* Number of minor devices this driver supports */
|
|
|
|
#define NUM_MINORS 256
|
|
|
|
|
|
|
|
/* Maximum size of any one send data buffer we support */
|
|
|
|
#define MAX_PACKET_SIZE (PAGE_SIZE * 2)
|
|
|
|
|
2015-05-12 02:16:37 +00:00
|
|
|
#define GB_LOOPBACK_MS_WAIT_MAX 1000
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
/* 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); \
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = connection->private; \
|
2015-03-31 07:51:59 +00:00
|
|
|
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); \
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = connection->private; \
|
2015-03-31 07:51:59 +00:00
|
|
|
return sprintf(buf, "%"#type"\n", gb->name.field); \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RO(name##_##field)
|
|
|
|
|
|
|
|
#define gb_loopback_stats_attrs(field) \
|
2015-08-03 17:57:14 +00:00
|
|
|
gb_loopback_ro_stats_attr(field, min, u); \
|
|
|
|
gb_loopback_ro_stats_attr(field, max, u); \
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_ro_stats_attr(field, avg, llu);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
#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); \
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = connection->private; \
|
2015-03-31 07:51:59 +00:00
|
|
|
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); \
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = connection->private; \
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_lock(&gb->mutex); \
|
2015-03-31 07:51:59 +00:00
|
|
|
ret = sscanf(buf, "%"#type, &gb->field); \
|
|
|
|
if (ret != 1) \
|
2015-07-28 17:34:36 +00:00
|
|
|
len = -EINVAL; \
|
|
|
|
else \
|
2015-07-28 17:34:39 +00:00
|
|
|
gb_loopback_check_attr(connection, gb); \
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_unlock(&gb->mutex); \
|
2015-03-31 07:51:59 +00:00
|
|
|
return len; \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RW(field)
|
|
|
|
|
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback *gb);
|
2015-07-28 17:34:39 +00:00
|
|
|
static void gb_loopback_check_attr(struct gb_connection *connection,
|
|
|
|
struct gb_loopback *gb)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-24 09:02:56 +00:00
|
|
|
if (gb->ms_wait > GB_LOOPBACK_MS_WAIT_MAX)
|
|
|
|
gb->ms_wait = GB_LOOPBACK_MS_WAIT_MAX;
|
|
|
|
if (gb->size > gb->size_max)
|
|
|
|
gb->size = gb->size_max;
|
|
|
|
gb->error = 0;
|
|
|
|
gb->iteration_count = 0;
|
|
|
|
gb_loopback_reset_stats(gb);
|
|
|
|
|
2015-07-28 17:34:39 +00:00
|
|
|
if (kfifo_depth < gb->iteration_max) {
|
|
|
|
dev_warn(&connection->dev,
|
|
|
|
"iteration_max %u kfifo_depth %u cannot log all data\n",
|
|
|
|
gb->iteration_max, kfifo_depth);
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:20:48 +00:00
|
|
|
switch (gb->type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
2015-07-13 19:20:49 +00:00
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
2015-07-28 17:34:38 +00:00
|
|
|
kfifo_reset_out(&gb->kfifo);
|
2015-07-24 09:02:56 +00:00
|
|
|
wake_up(&gb->wq);
|
2015-07-13 19:20:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gb->type = 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Time to send and receive one message */
|
|
|
|
gb_loopback_stats_attrs(latency);
|
2015-07-21 22:50:10 +00:00
|
|
|
/* Number of requests sent per second on this cport */
|
|
|
|
gb_loopback_stats_attrs(requests_per_second);
|
2015-03-31 07:51:59 +00:00
|
|
|
/* Quantity of data sent and received on this cport */
|
|
|
|
gb_loopback_stats_attrs(throughput);
|
2015-07-21 22:50:09 +00:00
|
|
|
/* Number of errors encountered during loop */
|
2015-03-31 07:51:59 +00:00
|
|
|
gb_loopback_ro_attr(error, d);
|
2015-07-21 22:50:09 +00:00
|
|
|
/* The current index of the for (i = 0; i < iteration_max; i++) loop */
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_ro_attr(iteration_count, u);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
/*
|
2015-07-21 22:50:07 +00:00
|
|
|
* Type of loopback message to send based on protocol type definitions
|
2015-03-31 07:51:59 +00:00
|
|
|
* 0 => Don't send message
|
2015-07-21 22:50:07 +00:00
|
|
|
* 2 => Send ping message continuously (message without payload)
|
2015-08-03 17:57:10 +00:00
|
|
|
* 3 => Send transfer message continuously (message with payload,
|
2015-07-21 22:50:07 +00:00
|
|
|
* payload returned in response)
|
|
|
|
* 4 => Send a sink message (message with payload, no payload in response)
|
2015-03-31 07:51:59 +00:00
|
|
|
*/
|
|
|
|
gb_loopback_attr(type, d);
|
|
|
|
/* Size of transfer message payload: 0-4096 bytes */
|
|
|
|
gb_loopback_attr(size, u);
|
2015-05-12 02:16:36 +00:00
|
|
|
/* Time to wait between two messages: 0-1000 ms */
|
2015-03-31 07:51:59 +00:00
|
|
|
gb_loopback_attr(ms_wait, d);
|
2015-07-21 22:50:08 +00:00
|
|
|
/* Maximum iterations for a given operation: 1-(2^32-1), 0 implies infinite */
|
|
|
|
gb_loopback_attr(iteration_max, u);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
#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),
|
2015-07-21 22:50:10 +00:00
|
|
|
dev_stats_attrs(requests_per_second),
|
2015-03-31 07:51:59 +00:00
|
|
|
dev_stats_attrs(throughput),
|
|
|
|
&dev_attr_type.attr,
|
|
|
|
&dev_attr_size.attr,
|
|
|
|
&dev_attr_ms_wait.attr,
|
2015-07-21 22:50:08 +00:00
|
|
|
&dev_attr_iteration_count.attr,
|
|
|
|
&dev_attr_iteration_max.attr,
|
2015-03-31 07:51:59 +00:00
|
|
|
&dev_attr_error.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(loopback);
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_sink(struct gb_loopback *gb, u32 len)
|
2015-07-13 19:20:49 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
struct gb_loopback_transfer_request *request;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
request = kmalloc(len + sizeof(*request), GFP_KERNEL);
|
|
|
|
if (!request)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
request->len = cpu_to_le32(len);
|
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
|
|
|
retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_SINK,
|
|
|
|
request, len + sizeof(*request), NULL, 0);
|
2015-07-13 19:20:50 +00:00
|
|
|
|
2015-07-13 19:20:49 +00:00
|
|
|
do_gettimeofday(&te);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-07-13 19:20:49 +00:00
|
|
|
|
|
|
|
kfree(request);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_transfer(struct gb_loopback *gb, u32 len)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
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);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
if (retval)
|
|
|
|
goto gb_error;
|
|
|
|
|
|
|
|
if (memcmp(request->data, response->data, len))
|
|
|
|
retval = -EREMOTEIO;
|
|
|
|
|
|
|
|
gb_error:
|
|
|
|
kfree(request);
|
|
|
|
kfree(response);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static int gb_loopback_ping(struct gb_loopback *gb)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
struct timeval ts, te;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
|
|
|
retval = gb_operation_sync(gb->connection, GB_LOOPBACK_TYPE_PING,
|
|
|
|
NULL, 0, NULL, 0);
|
|
|
|
do_gettimeofday(&te);
|
2015-07-28 17:34:35 +00:00
|
|
|
gb->elapsed_nsecs = timeval_to_ns(&te) - timeval_to_ns(&ts);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:16:39 +00:00
|
|
|
static int gb_loopback_request_recv(u8 type, struct gb_operation *operation)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = operation->connection;
|
2015-07-13 19:20:50 +00:00
|
|
|
struct gb_loopback *gb = connection->private;
|
2015-05-12 02:16:39 +00:00
|
|
|
struct gb_loopback_transfer_request *request;
|
|
|
|
struct gb_loopback_transfer_response *response;
|
2015-07-13 23:53:13 +00:00
|
|
|
size_t len;
|
2015-05-12 02:16:39 +00:00
|
|
|
|
|
|
|
/* By convention, the AP initiates the version operation */
|
|
|
|
switch (type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PROTOCOL_VERSION:
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"module-initiated version operation\n");
|
|
|
|
return -EINVAL;
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
2015-07-13 19:20:49 +00:00
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
2015-05-12 02:16:39 +00:00
|
|
|
return 0;
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
|
|
|
if (operation->request->payload_size < sizeof(*request)) {
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"transfer request too small (%zu < %zu)\n",
|
|
|
|
operation->request->payload_size,
|
|
|
|
sizeof(*request));
|
|
|
|
return -EINVAL; /* -EMSGSIZE */
|
|
|
|
}
|
|
|
|
request = operation->request->payload;
|
|
|
|
len = le32_to_cpu(request->len);
|
2015-07-13 19:20:50 +00:00
|
|
|
if (len > gb->size_max) {
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"transfer request too large (%zu > %zu)\n",
|
|
|
|
len, gb->size_max);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:16:39 +00:00
|
|
|
if (len) {
|
2015-07-17 16:50:25 +00:00
|
|
|
if (!gb_operation_response_alloc(operation, len,
|
|
|
|
GFP_KERNEL)) {
|
2015-05-12 02:16:39 +00:00
|
|
|
dev_err(&connection->dev,
|
|
|
|
"error allocating response\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
response = operation->response->payload;
|
|
|
|
memcpy(response->data, request->data, len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"unsupported request: %hhu\n", type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback *gb)
|
|
|
|
{
|
|
|
|
struct gb_loopback_stats reset = {
|
2015-08-03 17:57:14 +00:00
|
|
|
.min = U32_MAX,
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
memcpy(&gb->latency, &reset, sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb->throughput, &reset, sizeof(struct gb_loopback_stats));
|
2015-07-21 22:50:10 +00:00
|
|
|
memcpy(&gb->requests_per_second, &reset,
|
|
|
|
sizeof(struct gb_loopback_stats));
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 17:57:13 +00:00
|
|
|
static void gb_loopback_update_stats(struct gb_loopback_stats *stats, u32 val)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:08 +00:00
|
|
|
if (stats->min > val)
|
|
|
|
stats->min = val;
|
|
|
|
if (stats->max < val)
|
|
|
|
stats->max = val;
|
|
|
|
stats->sum += val;
|
|
|
|
stats->count++;
|
|
|
|
stats->avg = stats->sum;
|
|
|
|
do_div(stats->avg, stats->count);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:10 +00:00
|
|
|
u32 req = USEC_PER_SEC;
|
2015-07-21 22:50:08 +00:00
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
do_div(req, latency);
|
|
|
|
gb_loopback_update_stats(&gb->requests_per_second, req);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-07-21 22:50:08 +00:00
|
|
|
u32 throughput;
|
2015-07-13 19:20:51 +00:00
|
|
|
u32 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2;
|
|
|
|
|
|
|
|
switch (gb->type) {
|
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
|
|
|
gb->size;
|
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
|
|
|
sizeof(struct gb_loopback_transfer_response) +
|
|
|
|
gb->size * 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2015-07-21 22:50:08 +00:00
|
|
|
|
|
|
|
/* Calculate bytes per second */
|
|
|
|
throughput = USEC_PER_SEC;
|
|
|
|
do_div(throughput, latency);
|
|
|
|
throughput *= aggregate_size;
|
|
|
|
gb_loopback_update_stats(&gb->throughput, throughput);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:34:35 +00:00
|
|
|
static void gb_loopback_calculate_stats(struct gb_loopback *gb)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
u32 lat;
|
2015-04-16 07:53:59 +00:00
|
|
|
u64 tmp;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-21 22:50:08 +00:00
|
|
|
/* Express latency in terms of microseconds */
|
|
|
|
tmp = gb->elapsed_nsecs;
|
|
|
|
do_div(tmp, NSEC_PER_USEC);
|
2015-04-16 07:53:59 +00:00
|
|
|
lat = tmp;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-03 17:57:10 +00:00
|
|
|
/* Log latency statistic */
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_update_stats(&gb->latency, lat);
|
2015-07-28 17:34:38 +00:00
|
|
|
kfifo_in(&gb->kfifo, (unsigned char *)&lat, sizeof(lat));
|
2015-07-21 22:50:08 +00:00
|
|
|
|
2015-07-21 22:50:10 +00:00
|
|
|
/* Log throughput and requests using latency as benchmark */
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_throughput_update(gb, lat);
|
2015-07-21 22:50:10 +00:00
|
|
|
gb_loopback_requests_update(gb, lat);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_loopback_fn(void *data)
|
|
|
|
{
|
|
|
|
int error = 0;
|
2015-07-28 17:34:36 +00:00
|
|
|
int ms_wait;
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = data;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-24 09:02:56 +00:00
|
|
|
while (1) {
|
|
|
|
if (!gb->type)
|
|
|
|
wait_event_interruptible(gb->wq, gb->type ||
|
|
|
|
kthread_should_stop());
|
|
|
|
if (kthread_should_stop())
|
|
|
|
break;
|
2015-07-28 17:34:36 +00:00
|
|
|
|
|
|
|
mutex_lock(&gb->mutex);
|
2015-07-21 22:50:08 +00:00
|
|
|
if (gb->iteration_max) {
|
|
|
|
if (gb->iteration_count < gb->iteration_max) {
|
|
|
|
gb->iteration_count++;
|
2015-07-22 18:09:23 +00:00
|
|
|
sysfs_notify(&gb->connection->dev.kobj, NULL,
|
|
|
|
"iteration_count");
|
2015-07-21 22:50:08 +00:00
|
|
|
} else {
|
|
|
|
gb->type = 0;
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_unlock(&gb->mutex);
|
2015-07-21 22:50:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 19:20:48 +00:00
|
|
|
if (gb->type == GB_LOOPBACK_TYPE_PING)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_ping(gb);
|
2015-07-13 19:20:48 +00:00
|
|
|
else if (gb->type == GB_LOOPBACK_TYPE_TRANSFER)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_transfer(gb, gb->size);
|
2015-07-13 19:20:49 +00:00
|
|
|
else if (gb->type == GB_LOOPBACK_TYPE_SINK)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_sink(gb, gb->size);
|
2015-03-31 07:51:59 +00:00
|
|
|
if (error)
|
|
|
|
gb->error++;
|
2015-07-28 17:34:35 +00:00
|
|
|
gb_loopback_calculate_stats(gb);
|
2015-07-28 17:34:36 +00:00
|
|
|
ms_wait = gb->ms_wait;
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
if (ms_wait)
|
|
|
|
msleep(ms_wait);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_loopback_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb;
|
|
|
|
int retval;
|
2015-07-28 17:34:38 +00:00
|
|
|
int minor;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
gb = kzalloc(sizeof(*gb), GFP_KERNEL);
|
|
|
|
if (!gb)
|
|
|
|
return -ENOMEM;
|
2015-07-28 17:34:38 +00:00
|
|
|
gb_loopback_reset_stats(gb);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
gb->connection = connection;
|
|
|
|
connection->private = gb;
|
2015-03-31 21:02:34 +00:00
|
|
|
retval = sysfs_create_groups(&connection->dev.kobj, loopback_groups);
|
2015-03-31 07:51:59 +00:00
|
|
|
if (retval)
|
2015-05-14 16:03:04 +00:00
|
|
|
goto out_free;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
/* Get a minor number */
|
|
|
|
minor = ida_simple_get(&minors, 0, 0, GFP_KERNEL);
|
|
|
|
if (minor < 0) {
|
|
|
|
retval = minor;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
/* Check the version */
|
|
|
|
retval = get_version(gb);
|
|
|
|
if (retval)
|
2015-05-14 16:03:04 +00:00
|
|
|
goto out_get_ver;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-13 19:20:50 +00:00
|
|
|
/* Calculate maximum payload */
|
|
|
|
gb->size_max = gb_operation_get_payload_size_max(connection);
|
|
|
|
if (gb->size_max <= sizeof(struct gb_loopback_transfer_request)) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto out_get_ver;
|
|
|
|
}
|
|
|
|
gb->size_max -= sizeof(struct gb_loopback_transfer_request);
|
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
/* Allocate kfifo */
|
|
|
|
if (kfifo_alloc(&gb->kfifo, kfifo_depth * sizeof(u32),
|
|
|
|
GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto out_get_ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create device entry */
|
|
|
|
gb->dev = MKDEV(loopback_major, minor);
|
|
|
|
cdev_init(&gb->cdev, &loopback_fops);
|
|
|
|
retval = cdev_add(&gb->cdev, gb->dev, 1);
|
|
|
|
if (retval)
|
|
|
|
goto out_cdev;
|
|
|
|
|
|
|
|
gb->device = device_create(loopback_class, &connection->dev, gb->dev,
|
|
|
|
gb, "gb!loopback%d", minor);
|
|
|
|
if (IS_ERR(gb->device)) {
|
|
|
|
retval = PTR_ERR(gb->device);
|
|
|
|
goto out_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork worker thread */
|
2015-07-24 09:02:56 +00:00
|
|
|
init_waitqueue_head(&gb->wq);
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_init(&gb->mutex);
|
2015-03-31 07:51:59 +00:00
|
|
|
gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback");
|
|
|
|
if (IS_ERR(gb->task)) {
|
2015-05-12 02:16:35 +00:00
|
|
|
retval = PTR_ERR(gb->task);
|
2015-07-28 17:34:38 +00:00
|
|
|
goto out_kfifo;
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
out_device:
|
|
|
|
cdev_del(&gb->cdev);
|
|
|
|
out_cdev:
|
|
|
|
ida_simple_remove(&minors, minor);
|
|
|
|
out_kfifo:
|
|
|
|
kfifo_free(&gb->kfifo);
|
2015-05-14 16:03:04 +00:00
|
|
|
out_get_ver:
|
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_groups);
|
|
|
|
out_free:
|
2015-03-31 07:51:59 +00:00
|
|
|
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);
|
2015-07-28 17:34:38 +00:00
|
|
|
|
|
|
|
cdev_del(&gb->cdev);
|
|
|
|
ida_simple_remove(&minors, MINOR(gb->dev));
|
|
|
|
device_del(gb->device);
|
|
|
|
kfifo_free(&gb->kfifo);
|
2015-03-31 21:02:34 +00:00
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_groups);
|
2015-03-31 07:51:59 +00:00
|
|
|
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,
|
2015-05-12 02:16:39 +00:00
|
|
|
.request_recv = gb_loopback_request_recv,
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
static int loopback_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct cdev *cdev = inode->i_cdev;
|
|
|
|
struct gb_loopback *gb = container_of(cdev, struct gb_loopback, cdev);
|
|
|
|
|
|
|
|
file->private_data = gb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t loopback_read(struct file *file, char __user *buf, size_t count,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb = file->private_data;
|
|
|
|
size_t fifo_len;
|
|
|
|
int retval;
|
|
|
|
|
2015-08-03 17:57:10 +00:00
|
|
|
if (!count || count % sizeof(u32))
|
2015-07-28 17:34:38 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&gb->mutex);
|
|
|
|
fifo_len = kfifo_len(&gb->kfifo);
|
|
|
|
if (kfifo_to_user(&gb->kfifo, buf, min(fifo_len, count), &retval) != 0)
|
|
|
|
retval = -EIO;
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations loopback_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = loopback_read,
|
|
|
|
.open = loopback_open,
|
|
|
|
.llseek = noop_llseek,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int loopback_init(void)
|
|
|
|
{
|
|
|
|
dev_t dev;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
loopback_class = class_create(THIS_MODULE, "gb_loopback");
|
|
|
|
if (IS_ERR(loopback_class)) {
|
|
|
|
retval = PTR_ERR(loopback_class);
|
|
|
|
goto error_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = alloc_chrdev_region(&dev, 0, NUM_MINORS, "gb_loopback");
|
|
|
|
if (retval < 0)
|
|
|
|
goto error_chrdev;
|
|
|
|
|
|
|
|
loopback_major = MAJOR(dev);
|
|
|
|
|
|
|
|
retval = gb_protocol_register(&loopback_protocol);
|
|
|
|
if (retval)
|
|
|
|
goto error_gb;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_gb:
|
|
|
|
unregister_chrdev_region(dev, NUM_MINORS);
|
|
|
|
error_chrdev:
|
|
|
|
class_destroy(loopback_class);
|
|
|
|
error_class:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
module_init(loopback_init);
|
|
|
|
|
|
|
|
static void __exit loopback_exit(void)
|
|
|
|
{
|
|
|
|
gb_protocol_deregister(&loopback_protocol);
|
|
|
|
unregister_chrdev_region(MKDEV(loopback_major, 0), NUM_MINORS);
|
|
|
|
class_destroy(loopback_class);
|
|
|
|
ida_destroy(&minors);
|
|
|
|
}
|
|
|
|
module_exit(loopback_exit);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|