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-08-16 23:55:03 +00:00
|
|
|
#include <linux/debugfs.h>
|
2015-09-14 09:48:46 +00:00
|
|
|
#include <linux/list_sort.h>
|
2015-07-28 17:34:38 +00:00
|
|
|
|
2015-04-16 07:53:59 +00:00
|
|
|
#include <asm/div64.h>
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
#include "greybus.h"
|
|
|
|
|
2015-08-11 12:50:54 +00:00
|
|
|
#define NSEC_PER_DAY 86400000000000ULL
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
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 sum;
|
2015-08-03 17:57:15 +00:00
|
|
|
u32 count;
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
|
|
|
|
2015-08-16 23:55:03 +00:00
|
|
|
struct gb_loopback_device {
|
|
|
|
struct dentry *root;
|
2015-09-14 09:48:42 +00:00
|
|
|
struct dentry *file;
|
2015-08-16 23:55:03 +00:00
|
|
|
u32 count;
|
2015-08-16 23:55:04 +00:00
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
struct kfifo kfifo;
|
2015-08-16 23:55:04 +00:00
|
|
|
struct mutex mutex;
|
|
|
|
struct list_head list;
|
|
|
|
wait_queue_head_t wq;
|
|
|
|
|
|
|
|
int type;
|
2015-08-16 23:55:08 +00:00
|
|
|
u32 mask;
|
2015-08-16 23:55:04 +00:00
|
|
|
u32 size;
|
|
|
|
u32 iteration_max;
|
|
|
|
u32 iteration_count;
|
|
|
|
size_t size_max;
|
|
|
|
int ms_wait;
|
|
|
|
u32 error;
|
|
|
|
|
|
|
|
struct timeval start;
|
|
|
|
struct timeval end;
|
|
|
|
|
|
|
|
/* Overall stats */
|
|
|
|
struct gb_loopback_stats latency;
|
|
|
|
struct gb_loopback_stats throughput;
|
|
|
|
struct gb_loopback_stats requests_per_second;
|
2015-08-16 23:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct gb_loopback_device gb_dev;
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
struct gb_loopback {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
2015-08-16 23:55:03 +00:00
|
|
|
struct dentry *file;
|
2015-08-16 23:55:07 +00:00
|
|
|
struct kfifo kfifo_lat;
|
|
|
|
struct kfifo kfifo_ts;
|
2015-07-28 17:34:36 +00:00
|
|
|
struct mutex mutex;
|
2015-03-31 07:51:59 +00:00
|
|
|
struct task_struct *task;
|
2015-08-16 23:55:04 +00:00
|
|
|
struct list_head entry;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
/* Per connection stats */
|
2015-03-31 07:51:59 +00:00
|
|
|
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-08-16 23:55:04 +00:00
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
u32 lbid;
|
2015-08-16 23:55:04 +00:00
|
|
|
u32 iteration_count;
|
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 unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT;
|
|
|
|
module_param(kfifo_depth, uint, 0444);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* interface sysfs attributes */
|
2015-08-16 23:55:11 +00:00
|
|
|
#define gb_loopback_ro_attr(field, pfx, conn) \
|
|
|
|
static ssize_t field##_##pfx##_show(struct device *dev, \
|
2015-03-31 07:51:59 +00:00
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
2015-08-16 23:55:11 +00:00
|
|
|
struct gb_connection *connection; \
|
|
|
|
struct gb_loopback *gb; \
|
|
|
|
if (conn) { \
|
|
|
|
connection = to_gb_connection(dev); \
|
|
|
|
gb = connection->private; \
|
|
|
|
return sprintf(buf, "%u\n", gb->field); \
|
|
|
|
} else { \
|
|
|
|
return sprintf(buf, "%u\n", gb_dev.field); \
|
|
|
|
} \
|
2015-03-31 07:51:59 +00:00
|
|
|
} \
|
2015-08-16 23:55:11 +00:00
|
|
|
static DEVICE_ATTR_RO(field##_##pfx)
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
#define gb_loopback_ro_stats_attr(name, field, type, pfx, conn) \
|
|
|
|
static ssize_t name##_##field##_##pfx##_show(struct device *dev, \
|
2015-03-31 07:51:59 +00:00
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
2015-08-16 23:55:11 +00:00
|
|
|
struct gb_connection *connection; \
|
|
|
|
struct gb_loopback *gb; \
|
|
|
|
if (conn) { \
|
|
|
|
connection = to_gb_connection(dev); \
|
|
|
|
gb = connection->private; \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb->name.field); \
|
|
|
|
} else { \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb_dev.name.field); \
|
|
|
|
} \
|
2015-03-31 07:51:59 +00:00
|
|
|
} \
|
2015-08-16 23:55:11 +00:00
|
|
|
static DEVICE_ATTR_RO(name##_##field##_##pfx)
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
#define gb_loopback_ro_avg_attr(name, pfx, conn) \
|
|
|
|
static ssize_t name##_avg_##pfx##_show(struct device *dev, \
|
2015-08-03 17:57:18 +00:00
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
2015-08-16 23:55:11 +00:00
|
|
|
struct gb_loopback_stats *stats; \
|
|
|
|
struct gb_connection *connection; \
|
|
|
|
struct gb_loopback *gb; \
|
|
|
|
u64 avg; \
|
|
|
|
u32 count, rem; \
|
|
|
|
if (conn) { \
|
|
|
|
connection = to_gb_connection(dev); \
|
|
|
|
gb = connection->private; \
|
|
|
|
stats = &gb->name; \
|
|
|
|
} else { \
|
|
|
|
stats = &gb_dev.name; \
|
|
|
|
} \
|
|
|
|
count = stats->count ? stats->count : 1; \
|
|
|
|
avg = stats->sum + count / 2; /* round closest */ \
|
|
|
|
rem = do_div(avg, count); \
|
2015-08-03 17:57:19 +00:00
|
|
|
return sprintf(buf, "%llu.%06u\n", avg, 1000000 * rem / count); \
|
2015-08-03 17:57:18 +00:00
|
|
|
} \
|
2015-08-16 23:55:11 +00:00
|
|
|
static DEVICE_ATTR_RO(name##_avg_##pfx)
|
2015-08-03 17:57:18 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
#define gb_loopback_stats_attrs(field, pfx, conn) \
|
|
|
|
gb_loopback_ro_stats_attr(field, min, u, pfx, conn); \
|
|
|
|
gb_loopback_ro_stats_attr(field, max, u, pfx, conn); \
|
|
|
|
gb_loopback_ro_avg_attr(field, pfx, conn)
|
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-16 23:55:04 +00:00
|
|
|
mutex_lock(&gb_dev.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-08-16 23:55:04 +00:00
|
|
|
gb_loopback_check_attr(connection); \
|
|
|
|
mutex_unlock(&gb_dev.mutex); \
|
2015-03-31 07:51:59 +00:00
|
|
|
return len; \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RW(field)
|
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
#define gb_dev_loopback_ro_attr(field, conn) \
|
|
|
|
static ssize_t field##_show(struct device *dev, \
|
2015-08-16 23:55:04 +00:00
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
|
|
|
return sprintf(buf, "%u\n", gb_dev.field); \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RO(field)
|
|
|
|
|
|
|
|
#define gb_dev_loopback_rw_attr(field, type) \
|
|
|
|
static ssize_t field##_show(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
|
|
|
{ \
|
|
|
|
return sprintf(buf, "%"#type"\n", gb_dev.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); \
|
|
|
|
mutex_lock(&gb_dev.mutex); \
|
|
|
|
ret = sscanf(buf, "%"#type, &gb_dev.field); \
|
|
|
|
if (ret != 1) \
|
|
|
|
len = -EINVAL; \
|
|
|
|
else \
|
|
|
|
gb_loopback_check_attr(&gb_dev, connection); \
|
|
|
|
mutex_unlock(&gb_dev.mutex); \
|
|
|
|
return len; \
|
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RW(field)
|
|
|
|
|
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback_device *gb_dev);
|
|
|
|
static void gb_loopback_check_attr(struct gb_loopback_device *gb_dev,
|
|
|
|
struct gb_connection *connection)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
2015-08-16 23:55:04 +00:00
|
|
|
struct gb_loopback *gb;
|
|
|
|
|
|
|
|
if (gb_dev->ms_wait > GB_LOOPBACK_MS_WAIT_MAX)
|
|
|
|
gb_dev->ms_wait = GB_LOOPBACK_MS_WAIT_MAX;
|
|
|
|
if (gb_dev->size > gb_dev->size_max)
|
|
|
|
gb_dev->size = gb_dev->size_max;
|
|
|
|
gb_dev->iteration_count = 0;
|
|
|
|
gb_dev->error = 0;
|
|
|
|
|
|
|
|
list_for_each_entry(gb, &gb_dev->list, entry) {
|
|
|
|
mutex_lock(&gb->mutex);
|
|
|
|
gb->iteration_count = 0;
|
|
|
|
gb->error = 0;
|
|
|
|
if (kfifo_depth < gb_dev->iteration_max) {
|
|
|
|
dev_warn(&connection->dev,
|
|
|
|
"cannot log bytes %u kfifo_depth %u\n",
|
|
|
|
gb_dev->iteration_max, kfifo_depth);
|
|
|
|
}
|
2015-08-16 23:55:07 +00:00
|
|
|
kfifo_reset_out(&gb->kfifo_lat);
|
|
|
|
kfifo_reset_out(&gb->kfifo_ts);
|
2015-08-16 23:55:04 +00:00
|
|
|
mutex_unlock(&gb->mutex);
|
2015-07-28 17:34:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
switch (gb_dev->type) {
|
2015-07-13 19:20:48 +00:00
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
2015-07-13 19:20:49 +00:00
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
2015-08-16 23:55:07 +00:00
|
|
|
kfifo_reset_out(&gb_dev->kfifo);
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_loopback_reset_stats(gb_dev);
|
|
|
|
wake_up(&gb_dev->wq);
|
2015-07-13 19:20:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev->type = 0;
|
2015-07-13 19:20:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Time to send and receive one message */
|
2015-08-16 23:55:11 +00:00
|
|
|
gb_loopback_stats_attrs(latency, dev, false);
|
|
|
|
gb_loopback_stats_attrs(latency, con, true);
|
2015-07-21 22:50:10 +00:00
|
|
|
/* Number of requests sent per second on this cport */
|
2015-08-16 23:55:11 +00:00
|
|
|
gb_loopback_stats_attrs(requests_per_second, dev, false);
|
|
|
|
gb_loopback_stats_attrs(requests_per_second, con, true);
|
2015-03-31 07:51:59 +00:00
|
|
|
/* Quantity of data sent and received on this cport */
|
2015-08-16 23:55:11 +00:00
|
|
|
gb_loopback_stats_attrs(throughput, dev, false);
|
|
|
|
gb_loopback_stats_attrs(throughput, con, true);
|
2015-07-21 22:50:09 +00:00
|
|
|
/* Number of errors encountered during loop */
|
2015-08-16 23:55:11 +00:00
|
|
|
gb_loopback_ro_attr(error, dev, false);
|
|
|
|
gb_loopback_ro_attr(error, con, true);
|
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
|
|
|
*/
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev_loopback_rw_attr(type, d);
|
2015-03-31 07:51:59 +00:00
|
|
|
/* Size of transfer message payload: 0-4096 bytes */
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev_loopback_rw_attr(size, u);
|
2015-05-12 02:16:36 +00:00
|
|
|
/* Time to wait between two messages: 0-1000 ms */
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev_loopback_rw_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 */
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev_loopback_rw_attr(iteration_max, u);
|
|
|
|
/* The current index of the for (i = 0; i < iteration_max; i++) loop */
|
2015-08-16 23:55:11 +00:00
|
|
|
gb_dev_loopback_ro_attr(iteration_count, false);
|
2015-08-16 23:55:08 +00:00
|
|
|
/* A bit-mask of destination connecitons to include in the test run */
|
|
|
|
gb_dev_loopback_rw_attr(mask, u);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
static struct attribute *loopback_dev_attrs[] = {
|
|
|
|
&dev_attr_latency_min_dev.attr,
|
|
|
|
&dev_attr_latency_max_dev.attr,
|
|
|
|
&dev_attr_latency_avg_dev.attr,
|
|
|
|
&dev_attr_requests_per_second_min_dev.attr,
|
|
|
|
&dev_attr_requests_per_second_max_dev.attr,
|
|
|
|
&dev_attr_requests_per_second_avg_dev.attr,
|
|
|
|
&dev_attr_throughput_min_dev.attr,
|
|
|
|
&dev_attr_throughput_max_dev.attr,
|
|
|
|
&dev_attr_throughput_avg_dev.attr,
|
2015-03-31 07:51:59 +00:00
|
|
|
&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-08-16 23:55:08 +00:00
|
|
|
&dev_attr_mask.attr,
|
2015-08-16 23:55:11 +00:00
|
|
|
&dev_attr_error_dev.attr,
|
2015-03-31 07:51:59 +00:00
|
|
|
NULL,
|
|
|
|
};
|
2015-08-16 23:55:11 +00:00
|
|
|
ATTRIBUTE_GROUPS(loopback_dev);
|
|
|
|
|
|
|
|
static struct attribute *loopback_con_attrs[] = {
|
|
|
|
&dev_attr_latency_min_con.attr,
|
|
|
|
&dev_attr_latency_max_con.attr,
|
|
|
|
&dev_attr_latency_avg_con.attr,
|
|
|
|
&dev_attr_requests_per_second_min_con.attr,
|
|
|
|
&dev_attr_requests_per_second_max_con.attr,
|
|
|
|
&dev_attr_requests_per_second_avg_con.attr,
|
|
|
|
&dev_attr_throughput_min_con.attr,
|
|
|
|
&dev_attr_throughput_max_con.attr,
|
|
|
|
&dev_attr_throughput_avg_con.attr,
|
|
|
|
&dev_attr_error_con.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(loopback_con);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:05 +00:00
|
|
|
static u32 gb_loopback_nsec_to_usec_latency(u64 elapsed_nsecs)
|
|
|
|
{
|
|
|
|
u32 lat;
|
|
|
|
|
|
|
|
do_div(elapsed_nsecs, NSEC_PER_USEC);
|
|
|
|
lat = elapsed_nsecs;
|
|
|
|
return lat;
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:06 +00:00
|
|
|
static u64 __gb_loopback_calc_latency(u64 t1, u64 t2)
|
|
|
|
{
|
|
|
|
if (t2 > t1)
|
|
|
|
return t2 - t1;
|
|
|
|
else
|
|
|
|
return NSEC_PER_DAY - t2 + t1;
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:02 +00:00
|
|
|
static u64 gb_loopback_calc_latency(struct timeval *ts, struct timeval *te)
|
2015-08-11 12:50:53 +00:00
|
|
|
{
|
|
|
|
u64 t1, t2;
|
|
|
|
|
|
|
|
t1 = timeval_to_ns(ts);
|
|
|
|
t2 = timeval_to_ns(te);
|
2015-08-16 23:55:06 +00:00
|
|
|
|
|
|
|
return __gb_loopback_calc_latency(t1, t2);
|
2015-08-11 12:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
static void gb_loopback_push_latency_ts(struct gb_loopback *gb,
|
|
|
|
struct timeval *ts, struct timeval *te)
|
|
|
|
{
|
|
|
|
kfifo_in(&gb->kfifo_ts, (unsigned char *)ts, sizeof(*ts));
|
|
|
|
kfifo_in(&gb->kfifo_ts, (unsigned char *)te, sizeof(*te));
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:08 +00:00
|
|
|
static int gb_loopback_active(struct gb_loopback *gb)
|
|
|
|
{
|
|
|
|
return (gb_dev.mask == 0 || (gb_dev.mask & gb->lbid));
|
|
|
|
}
|
|
|
|
|
2015-09-14 09:48:47 +00:00
|
|
|
static int gb_loopback_operation_sync(struct gb_loopback *gb, int type,
|
|
|
|
void *request, int request_size,
|
|
|
|
void *response, int response_size)
|
2015-07-13 19:20:49 +00:00
|
|
|
{
|
2015-09-14 09:48:47 +00:00
|
|
|
struct gb_operation *operation;
|
2015-07-13 19:20:49 +00:00
|
|
|
struct timeval ts, te;
|
2015-09-14 09:48:47 +00:00
|
|
|
int ret;
|
2015-07-13 19:20:49 +00:00
|
|
|
|
|
|
|
do_gettimeofday(&ts);
|
2015-09-14 09:48:47 +00:00
|
|
|
operation = gb_operation_create(gb->connection, type, request_size,
|
|
|
|
response_size, GFP_KERNEL);
|
|
|
|
if (!operation) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
2015-07-13 19:20:50 +00:00
|
|
|
|
2015-09-14 09:48:47 +00:00
|
|
|
if (request_size)
|
|
|
|
memcpy(operation->request->payload, request, request_size);
|
|
|
|
|
|
|
|
ret = gb_operation_request_send_sync(operation);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&gb->connection->dev,
|
|
|
|
"synchronous operation failed: %d\n", ret);
|
|
|
|
} else {
|
|
|
|
if (response_size == operation->response->payload_size) {
|
|
|
|
memcpy(response, operation->response->payload,
|
|
|
|
response_size);
|
|
|
|
} else {
|
|
|
|
dev_err(&gb->connection->dev,
|
|
|
|
"response size %zu expected %d\n",
|
|
|
|
operation->response->payload_size,
|
|
|
|
response_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gb_operation_destroy(operation);
|
|
|
|
|
|
|
|
error:
|
2015-07-13 19:20:49 +00:00
|
|
|
do_gettimeofday(&te);
|
2015-08-16 23:55:02 +00:00
|
|
|
|
|
|
|
/* Calculate the total time the message took */
|
2015-08-16 23:55:07 +00:00
|
|
|
gb_loopback_push_latency_ts(gb, &ts, &te);
|
2015-08-16 23:55:02 +00:00
|
|
|
gb->elapsed_nsecs = gb_loopback_calc_latency(&ts, &te);
|
|
|
|
|
2015-09-14 09:48:47 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-07-13 19:20:49 +00:00
|
|
|
|
2015-09-14 09:48:47 +00:00
|
|
|
static int gb_loopback_sink(struct gb_loopback *gb, u32 len)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_SINK,
|
|
|
|
request, len + sizeof(*request),
|
|
|
|
NULL, 0);
|
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 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;
|
|
|
|
}
|
|
|
|
|
2015-08-14 11:37:10 +00:00
|
|
|
memset(request->data, 0x5A, len);
|
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
request->len = cpu_to_le32(len);
|
2015-09-14 09:48:47 +00:00
|
|
|
retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_TRANSFER,
|
|
|
|
request, len + sizeof(*request),
|
|
|
|
response, len + sizeof(*response));
|
2015-03-31 07:51:59 +00:00
|
|
|
if (retval)
|
|
|
|
goto gb_error;
|
|
|
|
|
2015-08-14 11:37:11 +00:00
|
|
|
if (memcmp(request->data, response->data, len)) {
|
2015-09-14 09:48:44 +00:00
|
|
|
dev_err(&gb->connection->dev, "Loopback Data doesn't match\n");
|
2015-03-31 07:51:59 +00:00
|
|
|
retval = -EREMOTEIO;
|
2015-08-14 11:37:11 +00:00
|
|
|
}
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-09-14 09:48:47 +00:00
|
|
|
return gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_PING,
|
|
|
|
NULL, 0, NULL, 0);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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) {
|
2015-08-14 02:27:38 +00:00
|
|
|
case GB_REQUEST_TYPE_PROTOCOL_VERSION:
|
2015-05-12 02:16:39 +00:00
|
|
|
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-08-16 23:55:04 +00:00
|
|
|
if (len > gb_dev.size_max) {
|
2015-07-13 19:20:50 +00:00
|
|
|
dev_err(&connection->dev,
|
|
|
|
"transfer request too large (%zu > %zu)\n",
|
2015-08-16 23:55:04 +00:00
|
|
|
len, gb_dev.size_max);
|
2015-07-13 19:20:50 +00:00
|
|
|
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;
|
2015-09-14 09:48:45 +00:00
|
|
|
response->len = cpu_to_le32(len);
|
2015-05-12 02:16:39 +00:00
|
|
|
memcpy(response->data, request->data, len);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
dev_err(&connection->dev,
|
|
|
|
"unsupported request: %hhu\n", type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
static void gb_loopback_reset_stats(struct gb_loopback_device *gb_dev)
|
2015-03-31 07:51:59 +00:00
|
|
|
{
|
|
|
|
struct gb_loopback_stats reset = {
|
2015-08-03 17:57:14 +00:00
|
|
|
.min = U32_MAX,
|
2015-03-31 07:51:59 +00:00
|
|
|
};
|
2015-08-16 23:55:04 +00:00
|
|
|
struct gb_loopback *gb;
|
|
|
|
|
|
|
|
/* Reset per-connection stats */
|
|
|
|
list_for_each_entry(gb, &gb_dev->list, entry) {
|
|
|
|
mutex_lock(&gb->mutex);
|
|
|
|
memcpy(&gb->latency, &reset,
|
|
|
|
sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb->throughput, &reset,
|
|
|
|
sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb->requests_per_second, &reset,
|
|
|
|
sizeof(struct gb_loopback_stats));
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset aggregate stats */
|
|
|
|
memset(&gb_dev->start, 0, sizeof(struct timeval));
|
|
|
|
memset(&gb_dev->end, 0, sizeof(struct timeval));
|
|
|
|
memcpy(&gb_dev->latency, &reset, sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb_dev->throughput, &reset, sizeof(struct gb_loopback_stats));
|
|
|
|
memcpy(&gb_dev->requests_per_second, &reset,
|
2015-07-21 22:50:10 +00:00
|
|
|
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++;
|
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);
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_loopback_update_stats(&gb_dev.requests_per_second, req);
|
2015-07-21 22:50:10 +00:00
|
|
|
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;
|
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
switch (gb_dev.type) {
|
2015-07-13 19:20:51 +00:00
|
|
|
case GB_LOOPBACK_TYPE_PING:
|
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_SINK:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev.size;
|
2015-07-13 19:20:51 +00:00
|
|
|
break;
|
|
|
|
case GB_LOOPBACK_TYPE_TRANSFER:
|
|
|
|
aggregate_size += sizeof(struct gb_loopback_transfer_request) +
|
|
|
|
sizeof(struct gb_loopback_transfer_response) +
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev.size * 2;
|
2015-07-13 19:20:51 +00:00
|
|
|
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;
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_loopback_update_stats(&gb_dev.throughput, throughput);
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_update_stats(&gb->throughput, throughput);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
static int gb_loopback_calculate_aggregate_stats(void)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb;
|
|
|
|
struct timeval ts;
|
|
|
|
struct timeval te;
|
|
|
|
u64 t1, t2;
|
|
|
|
u64 ts_min;
|
|
|
|
u64 te_max;
|
|
|
|
u64 elapsed_nsecs;
|
|
|
|
u32 lat;
|
|
|
|
int i, latched;
|
|
|
|
int rollover = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < gb_dev.iteration_max; i++) {
|
|
|
|
latched = 0;
|
|
|
|
ts_min = 0;
|
|
|
|
te_max = 0;
|
|
|
|
list_for_each_entry(gb, &gb_dev.list, entry) {
|
2015-08-16 23:55:08 +00:00
|
|
|
if (!gb_loopback_active(gb))
|
|
|
|
continue;
|
2015-08-16 23:55:07 +00:00
|
|
|
if (kfifo_out(&gb->kfifo_ts, &ts, sizeof(ts)) < sizeof(ts))
|
|
|
|
goto error;
|
|
|
|
if (kfifo_out(&gb->kfifo_ts, &te, sizeof(te)) < sizeof(te))
|
|
|
|
goto error;
|
|
|
|
t1 = timeval_to_ns(&ts);
|
|
|
|
t2 = timeval_to_ns(&te);
|
|
|
|
|
|
|
|
/* minimum timestamp is always what we want */
|
|
|
|
if (latched == 0 || t1 < ts_min)
|
|
|
|
ts_min = t1;
|
|
|
|
|
|
|
|
/* maximum timestamp needs to handle rollover */
|
|
|
|
if (t2 > t1) {
|
|
|
|
if (latched == 0 || t2 > te_max)
|
|
|
|
te_max = t2;
|
|
|
|
} else {
|
|
|
|
if (latched == 0 || rollover == 0)
|
|
|
|
te_max = t2;
|
|
|
|
if (rollover == 1 && t2 > te_max)
|
|
|
|
te_max = t2;
|
|
|
|
rollover = 1;
|
|
|
|
}
|
|
|
|
latched = 1;
|
|
|
|
}
|
|
|
|
/* Calculate the aggregate timestamp */
|
|
|
|
elapsed_nsecs = __gb_loopback_calc_latency(ts_min, te_max);
|
|
|
|
lat = gb_loopback_nsec_to_usec_latency(elapsed_nsecs);
|
|
|
|
kfifo_in(&gb_dev.kfifo, (unsigned char *)&lat, sizeof(lat));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
error:
|
|
|
|
kfifo_reset_out(&gb_dev.kfifo);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
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-07-21 22:50:08 +00:00
|
|
|
/* Express latency in terms of microseconds */
|
2015-08-16 23:55:05 +00:00
|
|
|
lat = gb_loopback_nsec_to_usec_latency(gb->elapsed_nsecs);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:12 +00:00
|
|
|
/* Log latency stastic */
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_loopback_update_stats(&gb_dev.latency, lat);
|
2015-07-21 22:50:08 +00:00
|
|
|
gb_loopback_update_stats(&gb->latency, lat);
|
2015-08-16 23:55:04 +00:00
|
|
|
|
|
|
|
/* Raw latency log on a per thread basis */
|
2015-08-16 23:55:07 +00:00
|
|
|
kfifo_in(&gb->kfifo_lat, (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-08-16 23:55:09 +00:00
|
|
|
int ms_wait = 0;
|
2015-08-16 23:55:04 +00:00
|
|
|
int type;
|
|
|
|
u32 size;
|
|
|
|
u32 low_count;
|
2015-08-03 17:57:11 +00:00
|
|
|
struct gb_loopback *gb = data;
|
2015-08-16 23:55:04 +00:00
|
|
|
struct gb_loopback *gb_list;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-07-24 09:02:56 +00:00
|
|
|
while (1) {
|
2015-08-16 23:55:04 +00:00
|
|
|
if (!gb_dev.type)
|
|
|
|
wait_event_interruptible(gb_dev.wq, gb_dev.type ||
|
2015-07-24 09:02:56 +00:00
|
|
|
kthread_should_stop());
|
|
|
|
if (kthread_should_stop())
|
|
|
|
break;
|
2015-07-28 17:34:36 +00:00
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
mutex_lock(&gb_dev.mutex);
|
2015-09-23 16:31:32 +00:00
|
|
|
if (!gb_loopback_active(gb)) {
|
|
|
|
ms_wait = 100;
|
2015-08-16 23:55:08 +00:00
|
|
|
goto unlock_continue;
|
2015-09-23 16:31:32 +00:00
|
|
|
}
|
2015-08-16 23:55:04 +00:00
|
|
|
if (gb_dev.iteration_max) {
|
|
|
|
/* Determine overall lowest count */
|
|
|
|
low_count = gb->iteration_count;
|
|
|
|
list_for_each_entry(gb_list, &gb_dev.list, entry) {
|
2015-08-16 23:55:08 +00:00
|
|
|
if (!gb_loopback_active(gb_list))
|
|
|
|
continue;
|
2015-08-16 23:55:04 +00:00
|
|
|
if (gb_list->iteration_count < low_count)
|
|
|
|
low_count = gb_list->iteration_count;
|
|
|
|
}
|
|
|
|
/* All threads achieved at least low_count iterations */
|
|
|
|
if (gb_dev.iteration_count < low_count) {
|
|
|
|
gb_dev.iteration_count = low_count;
|
2015-07-22 18:09:23 +00:00
|
|
|
sysfs_notify(&gb->connection->dev.kobj, NULL,
|
|
|
|
"iteration_count");
|
2015-08-16 23:55:04 +00:00
|
|
|
}
|
|
|
|
/* Optionally terminate */
|
|
|
|
if (gb_dev.iteration_count == gb_dev.iteration_max) {
|
2015-08-16 23:55:07 +00:00
|
|
|
gb_loopback_calculate_aggregate_stats();
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_dev.type = 0;
|
2015-08-16 23:55:08 +00:00
|
|
|
goto unlock_continue;
|
2015-07-21 22:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 23:55:04 +00:00
|
|
|
size = gb_dev.size;
|
|
|
|
ms_wait = gb_dev.ms_wait;
|
|
|
|
type = gb_dev.type;
|
|
|
|
mutex_unlock(&gb_dev.mutex);
|
|
|
|
|
|
|
|
mutex_lock(&gb->mutex);
|
|
|
|
if (gb->iteration_count >= gb_dev.iteration_max) {
|
|
|
|
/* If this thread finished before siblings then sleep */
|
|
|
|
ms_wait = 1;
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
goto sleep;
|
|
|
|
}
|
|
|
|
/* Else operations to perform */
|
|
|
|
if (type == GB_LOOPBACK_TYPE_PING)
|
2015-07-28 17:34:35 +00:00
|
|
|
error = gb_loopback_ping(gb);
|
2015-08-16 23:55:04 +00:00
|
|
|
else if (type == GB_LOOPBACK_TYPE_TRANSFER)
|
|
|
|
error = gb_loopback_transfer(gb, size);
|
|
|
|
else if (type == GB_LOOPBACK_TYPE_SINK)
|
|
|
|
error = gb_loopback_sink(gb, size);
|
|
|
|
mutex_unlock(&gb->mutex);
|
|
|
|
|
|
|
|
mutex_lock(&gb_dev.mutex);
|
|
|
|
mutex_lock(&gb->mutex);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
gb_dev.error++;
|
2015-03-31 07:51:59 +00:00
|
|
|
gb->error++;
|
2015-08-16 23:55:04 +00:00
|
|
|
}
|
2015-07-28 17:34:35 +00:00
|
|
|
gb_loopback_calculate_stats(gb);
|
2015-08-16 23:55:04 +00:00
|
|
|
gb->iteration_count++;
|
|
|
|
|
2015-07-28 17:34:36 +00:00
|
|
|
mutex_unlock(&gb->mutex);
|
2015-08-16 23:55:08 +00:00
|
|
|
unlock_continue:
|
2015-08-16 23:55:04 +00:00
|
|
|
mutex_unlock(&gb_dev.mutex);
|
|
|
|
sleep:
|
2015-07-28 17:34:36 +00:00
|
|
|
if (ms_wait)
|
|
|
|
msleep(ms_wait);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
static int gb_loopback_dbgfs_latency_show_common(struct seq_file *s,
|
|
|
|
struct kfifo *kfifo,
|
|
|
|
struct mutex *mutex)
|
2015-08-16 23:55:03 +00:00
|
|
|
{
|
|
|
|
u32 latency;
|
|
|
|
int retval;
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
if (kfifo_len(kfifo) == 0) {
|
2015-08-16 23:55:03 +00:00
|
|
|
retval = -EAGAIN;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
mutex_lock(mutex);
|
|
|
|
retval = kfifo_out(kfifo, &latency, sizeof(latency));
|
2015-08-16 23:55:03 +00:00
|
|
|
if (retval > 0) {
|
|
|
|
seq_printf(s, "%u", latency);
|
|
|
|
retval = 0;
|
|
|
|
}
|
2015-08-16 23:55:07 +00:00
|
|
|
mutex_unlock(mutex);
|
2015-08-16 23:55:03 +00:00
|
|
|
done:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
static int gb_loopback_dbgfs_latency_show(struct seq_file *s, void *unused)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb = s->private;
|
|
|
|
|
|
|
|
return gb_loopback_dbgfs_latency_show_common(s, &gb->kfifo_lat,
|
|
|
|
&gb->mutex);
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:03 +00:00
|
|
|
static int gb_loopback_latency_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, gb_loopback_dbgfs_latency_show,
|
|
|
|
inode->i_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations gb_loopback_debugfs_latency_ops = {
|
|
|
|
.open = gb_loopback_latency_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
static int gb_loopback_dbgfs_dev_latency_show(struct seq_file *s, void *unused)
|
|
|
|
{
|
|
|
|
struct gb_loopback_device *gb_dev = s->private;
|
|
|
|
|
|
|
|
return gb_loopback_dbgfs_latency_show_common(s, &gb_dev->kfifo,
|
|
|
|
&gb_dev->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_loopback_dev_latency_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, gb_loopback_dbgfs_dev_latency_show,
|
|
|
|
inode->i_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations gb_loopback_debugfs_dev_latency_ops = {
|
|
|
|
.open = gb_loopback_dev_latency_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
2015-09-14 09:48:46 +00:00
|
|
|
static int gb_loopback_bus_id_compare(void *priv, struct list_head *lha,
|
|
|
|
struct list_head *lhb)
|
|
|
|
{
|
|
|
|
struct gb_loopback *a = list_entry(lha, struct gb_loopback, entry);
|
|
|
|
struct gb_loopback *b = list_entry(lhb, struct gb_loopback, entry);
|
|
|
|
struct gb_connection *ca = a->connection;
|
|
|
|
struct gb_connection *cb = b->connection;
|
|
|
|
|
|
|
|
if (ca->bundle->intf->module->module_id <
|
|
|
|
cb->bundle->intf->module->module_id)
|
|
|
|
return -1;
|
|
|
|
if (cb->bundle->intf->module->module_id <
|
|
|
|
ca->bundle->intf->module->module_id)
|
|
|
|
return 1;
|
|
|
|
if (ca->bundle->intf->interface_id < cb->bundle->intf->interface_id)
|
|
|
|
return -1;
|
|
|
|
if (cb->bundle->intf->interface_id < ca->bundle->intf->interface_id)
|
|
|
|
return 1;
|
|
|
|
if (ca->bundle->id < cb->bundle->id)
|
|
|
|
return -1;
|
|
|
|
if (cb->bundle->id < ca->bundle->id)
|
|
|
|
return 1;
|
|
|
|
if (ca->intf_cport_id < cb->intf_cport_id)
|
|
|
|
return -1;
|
|
|
|
else if (cb->intf_cport_id < ca->intf_cport_id)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_loopback_insert_id(struct gb_loopback *gb)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb_list;
|
|
|
|
u32 new_lbid = 0;
|
|
|
|
|
|
|
|
/* perform an insertion sort */
|
|
|
|
list_add_tail(&gb->entry, &gb_dev.list);
|
|
|
|
list_sort(NULL, &gb_dev.list, gb_loopback_bus_id_compare);
|
|
|
|
list_for_each_entry(gb_list, &gb_dev.list, entry) {
|
|
|
|
gb_list->lbid = 1 << new_lbid;
|
|
|
|
new_lbid++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
#define DEBUGFS_NAMELEN 32
|
2015-08-16 23:55:03 +00:00
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
static int gb_loopback_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb;
|
|
|
|
int retval;
|
2015-08-16 23:55:03 +00:00
|
|
|
char name[DEBUGFS_NAMELEN];
|
2015-09-23 16:31:31 +00:00
|
|
|
struct kobject *kobj = &connection->hd->endo->dev.kobj;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
gb = kzalloc(sizeof(*gb), GFP_KERNEL);
|
|
|
|
if (!gb)
|
|
|
|
return -ENOMEM;
|
2015-08-16 23:55:04 +00:00
|
|
|
gb_loopback_reset_stats(&gb_dev);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
/* If this is the first connection - create a module endo0:x entries */
|
|
|
|
mutex_lock(&gb_dev.mutex);
|
|
|
|
if (!gb_dev.count) {
|
|
|
|
snprintf(name, sizeof(name), "raw_latency_endo0:%d",
|
|
|
|
connection->bundle->intf->module->module_id);
|
2015-09-14 09:48:42 +00:00
|
|
|
gb_dev.file = debugfs_create_file(name, S_IFREG | S_IRUGO,
|
|
|
|
gb_dev.root, &gb_dev,
|
|
|
|
&gb_loopback_debugfs_dev_latency_ops);
|
2015-08-16 23:55:11 +00:00
|
|
|
retval = sysfs_create_groups(kobj, loopback_dev_groups);
|
2015-09-14 09:48:40 +00:00
|
|
|
if (retval)
|
2015-08-16 23:55:11 +00:00
|
|
|
goto out_sysfs;
|
2015-09-14 09:48:40 +00:00
|
|
|
|
2015-08-16 23:55:11 +00:00
|
|
|
/* Calculate maximum payload */
|
|
|
|
gb_dev.size_max = gb_operation_get_payload_size_max(connection);
|
|
|
|
if (gb_dev.size_max <=
|
|
|
|
sizeof(struct gb_loopback_transfer_request)) {
|
|
|
|
retval = -EINVAL;
|
2015-09-14 09:48:43 +00:00
|
|
|
goto out_sysfs_dev;
|
2015-08-16 23:55:11 +00:00
|
|
|
}
|
|
|
|
gb_dev.size_max -= sizeof(struct gb_loopback_transfer_request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create per-connection sysfs and debugfs data-points */
|
2015-09-14 09:48:55 +00:00
|
|
|
snprintf(name, sizeof(name), "raw_latency_endo0:%s",
|
|
|
|
dev_name(&connection->dev));
|
2015-08-16 23:55:03 +00:00
|
|
|
gb->file = debugfs_create_file(name, S_IFREG | S_IRUGO, gb_dev.root, gb,
|
|
|
|
&gb_loopback_debugfs_latency_ops);
|
2015-03-31 07:51:59 +00:00
|
|
|
gb->connection = connection;
|
|
|
|
connection->private = gb;
|
2015-08-16 23:55:11 +00:00
|
|
|
retval = sysfs_create_groups(&connection->dev.kobj,
|
|
|
|
loopback_con_groups);
|
2015-03-31 07:51:59 +00:00
|
|
|
if (retval)
|
2015-08-16 23:55:11 +00:00
|
|
|
goto out_sysfs_dev;
|
2015-07-13 19:20:50 +00:00
|
|
|
|
2015-07-28 17:34:38 +00:00
|
|
|
/* Allocate kfifo */
|
2015-08-16 23:55:07 +00:00
|
|
|
if (kfifo_alloc(&gb->kfifo_lat, kfifo_depth * sizeof(u32),
|
2015-07-28 17:34:38 +00:00
|
|
|
GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
2015-08-16 23:55:11 +00:00
|
|
|
goto out_sysfs_conn;
|
2015-07-28 17:34:38 +00:00
|
|
|
}
|
2015-08-16 23:55:07 +00:00
|
|
|
if (kfifo_alloc(&gb->kfifo_ts, kfifo_depth * sizeof(struct timeval) * 2,
|
|
|
|
GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto out_kfifo0;
|
|
|
|
}
|
2015-07-28 17:34:38 +00:00
|
|
|
|
|
|
|
/* Fork worker thread */
|
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-08-16 23:55:07 +00:00
|
|
|
goto out_kfifo1;
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 09:48:46 +00:00
|
|
|
gb_loopback_insert_id(gb);
|
2015-08-16 23:55:03 +00:00
|
|
|
gb_dev.count++;
|
2015-09-14 09:48:40 +00:00
|
|
|
mutex_unlock(&gb_dev.mutex);
|
2015-03-31 07:51:59 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
out_kfifo1:
|
|
|
|
kfifo_free(&gb->kfifo_ts);
|
|
|
|
out_kfifo0:
|
|
|
|
kfifo_free(&gb->kfifo_lat);
|
2015-08-16 23:55:11 +00:00
|
|
|
out_sysfs_conn:
|
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_con_groups);
|
|
|
|
out_sysfs_dev:
|
2015-09-14 09:48:42 +00:00
|
|
|
if (!gb_dev.count) {
|
2015-08-16 23:55:11 +00:00
|
|
|
sysfs_remove_groups(kobj, loopback_dev_groups);
|
2015-09-14 09:48:42 +00:00
|
|
|
debugfs_remove(gb_dev.file);
|
|
|
|
}
|
2015-08-16 23:55:03 +00:00
|
|
|
debugfs_remove(gb->file);
|
2015-08-03 17:57:20 +00:00
|
|
|
connection->private = NULL;
|
2015-08-16 23:55:11 +00:00
|
|
|
out_sysfs:
|
2015-09-14 09:48:40 +00:00
|
|
|
mutex_unlock(&gb_dev.mutex);
|
2015-03-31 07:51:59 +00:00
|
|
|
kfree(gb);
|
2015-08-03 17:57:20 +00:00
|
|
|
|
2015-03-31 07:51:59 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_loopback_connection_exit(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_loopback *gb = connection->private;
|
2015-09-23 16:31:31 +00:00
|
|
|
struct kobject *kobj = &connection->hd->endo->dev.kobj;
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
if (!IS_ERR_OR_NULL(gb->task))
|
|
|
|
kthread_stop(gb->task);
|
2015-07-28 17:34:38 +00:00
|
|
|
|
2015-09-04 15:53:31 +00:00
|
|
|
mutex_lock(&gb_dev.mutex);
|
|
|
|
|
|
|
|
connection->private = NULL;
|
2015-08-16 23:55:07 +00:00
|
|
|
kfifo_free(&gb->kfifo_lat);
|
|
|
|
kfifo_free(&gb->kfifo_ts);
|
2015-09-14 09:48:39 +00:00
|
|
|
gb_dev.count--;
|
2015-09-14 09:48:42 +00:00
|
|
|
if (!gb_dev.count) {
|
2015-08-16 23:55:11 +00:00
|
|
|
sysfs_remove_groups(kobj, loopback_dev_groups);
|
2015-09-14 09:48:42 +00:00
|
|
|
debugfs_remove(gb_dev.file);
|
|
|
|
}
|
2015-08-16 23:55:11 +00:00
|
|
|
sysfs_remove_groups(&connection->dev.kobj, loopback_con_groups);
|
2015-08-16 23:55:03 +00:00
|
|
|
debugfs_remove(gb->file);
|
2015-09-04 15:53:31 +00:00
|
|
|
list_del(&gb->entry);
|
|
|
|
mutex_unlock(&gb_dev.mutex);
|
2015-09-14 09:48:41 +00:00
|
|
|
kfree(gb);
|
2015-03-31 07:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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_init(void)
|
|
|
|
{
|
2015-08-16 23:55:07 +00:00
|
|
|
int retval;
|
|
|
|
|
2015-08-16 23:55:04 +00:00
|
|
|
init_waitqueue_head(&gb_dev.wq);
|
|
|
|
INIT_LIST_HEAD(&gb_dev.list);
|
|
|
|
mutex_init(&gb_dev.mutex);
|
2015-08-16 23:55:03 +00:00
|
|
|
gb_dev.root = debugfs_create_dir("gb_loopback", NULL);
|
2015-08-16 23:55:04 +00:00
|
|
|
|
2015-08-16 23:55:07 +00:00
|
|
|
if (kfifo_alloc(&gb_dev.kfifo, kfifo_depth * sizeof(u32), GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto error_debugfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = gb_protocol_register(&loopback_protocol);
|
|
|
|
if (!retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
error_debugfs:
|
|
|
|
debugfs_remove_recursive(gb_dev.root);
|
|
|
|
return retval;
|
2015-07-28 17:34:38 +00:00
|
|
|
}
|
|
|
|
module_init(loopback_init);
|
|
|
|
|
|
|
|
static void __exit loopback_exit(void)
|
|
|
|
{
|
2015-08-16 23:55:03 +00:00
|
|
|
debugfs_remove_recursive(gb_dev.root);
|
2015-08-16 23:55:07 +00:00
|
|
|
kfifo_free(&gb_dev.kfifo);
|
2015-07-28 17:34:38 +00:00
|
|
|
gb_protocol_deregister(&loopback_protocol);
|
|
|
|
}
|
|
|
|
module_exit(loopback_exit);
|
2015-03-31 07:51:59 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|