blob: 7882306adeca9e3737a90003a806c06185cdc0ea [file] [log] [blame]
Alexandre Bailon355a7052015-03-31 09:51:59 +02001/*
2 * Loopback bridge driver for the Greybus loopback module.
3 *
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
Johan Hovold8923c5b2016-02-11 13:52:49 +01009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Alexandre Bailon355a7052015-03-31 09:51:59 +020012#include <linux/kernel.h>
13#include <linux/module.h>
Bryan O'Donoghue85d678c2015-07-28 18:34:36 +010014#include <linux/mutex.h>
Alexandre Bailon355a7052015-03-31 09:51:59 +020015#include <linux/slab.h>
16#include <linux/kthread.h>
17#include <linux/delay.h>
18#include <linux/random.h>
Greg Kroah-Hartman5679f782015-03-31 23:01:45 +020019#include <linux/sizes.h>
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +010020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/kfifo.h>
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +010023#include <linux/debugfs.h>
Bryan O'Donoghue50151152015-09-14 10:48:46 +010024#include <linux/list_sort.h>
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +000025#include <linux/spinlock.h>
Bryan O'Donoghue12927832015-12-07 01:59:06 +000026#include <linux/workqueue.h>
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +000027#include <linux/atomic.h>
Axel Haslame854ff52016-07-14 15:13:00 -050028#include <linux/pm_runtime.h>
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +010029
Johan Hovold1ffc12b2015-04-16 09:53:59 +020030#include <asm/div64.h>
31
Alexandre Bailon355a7052015-03-31 09:51:59 +020032#include "greybus.h"
Bryan O'Donoghuefd589262015-10-15 16:10:43 +010033#include "connection.h"
Alexandre Bailon355a7052015-03-31 09:51:59 +020034
Bryan O'Donoghuefd489e12015-08-11 13:50:54 +010035#define NSEC_PER_DAY 86400000000000ULL
36
Alexandre Bailon355a7052015-03-31 09:51:59 +020037struct gb_loopback_stats {
Alex Eldere051c822015-08-03 12:57:14 -050038 u32 min;
39 u32 max;
Bryan O'Donoghue00af6582015-07-21 23:50:08 +010040 u64 sum;
Alex Eldere13fc282015-08-03 12:57:15 -050041 u32 count;
Alexandre Bailon355a7052015-03-31 09:51:59 +020042};
43
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +010044struct gb_loopback_device {
45 struct dentry *root;
46 u32 count;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +000047 size_t size_max;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010048
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +000049 /* We need to take a lock in atomic context */
50 spinlock_t lock;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010051 struct list_head list;
Bryan O'Donoghue12927832015-12-07 01:59:06 +000052 struct list_head list_op_async;
53 wait_queue_head_t wq;
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +010054};
55
56static struct gb_loopback_device gb_dev;
57
Bryan O'Donoghue12927832015-12-07 01:59:06 +000058struct gb_loopback_async_operation {
59 struct gb_loopback *gb;
60 struct gb_operation *operation;
61 struct timeval ts;
62 struct timer_list timer;
63 struct list_head entry;
64 struct work_struct work;
65 struct kref kref;
66 bool pending;
67 int (*completion)(struct gb_loopback_async_operation *op_async);
68};
69
Alexandre Bailon355a7052015-03-31 09:51:59 +020070struct gb_loopback {
71 struct gb_connection *connection;
Alexandre Bailon355a7052015-03-31 09:51:59 +020072
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +010073 struct dentry *file;
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +010074 struct kfifo kfifo_lat;
75 struct kfifo kfifo_ts;
Bryan O'Donoghue85d678c2015-07-28 18:34:36 +010076 struct mutex mutex;
Alexandre Bailon355a7052015-03-31 09:51:59 +020077 struct task_struct *task;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010078 struct list_head entry;
Axel Haslam079fa322015-12-11 11:49:45 +010079 struct device *dev;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +000080 wait_queue_head_t wq;
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +000081 wait_queue_head_t wq_completion;
82 atomic_t outstanding_operations;
Alexandre Bailon355a7052015-03-31 09:51:59 +020083
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010084 /* Per connection stats */
Alexandre Bailonab81bb92016-02-25 18:19:15 +010085 struct timeval ts;
Alexandre Bailon355a7052015-03-31 09:51:59 +020086 struct gb_loopback_stats latency;
87 struct gb_loopback_stats throughput;
Bryan O'Donoghue583cbf52015-07-21 23:50:10 +010088 struct gb_loopback_stats requests_per_second;
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +010089 struct gb_loopback_stats apbridge_unipro_latency;
Sandeep Patile54b1062016-05-19 08:52:39 -070090 struct gb_loopback_stats gbphy_firmware_latency;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010091
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +000092 int type;
Bryan O'Donoghue12927832015-12-07 01:59:06 +000093 int async;
Axel Haslam079fa322015-12-11 11:49:45 +010094 int id;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +000095 u32 size;
96 u32 iteration_max;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +010097 u32 iteration_count;
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +000098 int us_wait;
Alexandre Bailon355a7052015-03-31 09:51:59 +020099 u32 error;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000100 u32 requests_completed;
101 u32 requests_timedout;
102 u32 timeout;
103 u32 jiffy_timeout;
104 u32 timeout_min;
105 u32 timeout_max;
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +0000106 u32 outstanding_operations_max;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000107 u32 lbid;
108 u64 elapsed_nsecs;
Bryan O'Donoghuee6227ee2015-10-15 16:10:44 +0100109 u32 apbridge_latency_ts;
Sandeep Patile54b1062016-05-19 08:52:39 -0700110 u32 gbphy_latency_ts;
Alexandre Bailon01480ba32016-03-08 17:40:16 +0100111
112 u32 send_count;
Alexandre Bailon355a7052015-03-31 09:51:59 +0200113};
114
Axel Haslam079fa322015-12-11 11:49:45 +0100115static struct class loopback_class = {
116 .name = "gb_loopback",
117 .owner = THIS_MODULE,
118};
119static DEFINE_IDA(loopback_ida);
120
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000121/* Min/max values in jiffies */
122#define GB_LOOPBACK_TIMEOUT_MIN 1
123#define GB_LOOPBACK_TIMEOUT_MAX 10000
124
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +0100125#define GB_LOOPBACK_FIFO_DEFAULT 8192
126
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +0100127static unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT;
128module_param(kfifo_depth, uint, 0444);
129
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +0100130/* Maximum size of any one send data buffer we support */
131#define MAX_PACKET_SIZE (PAGE_SIZE * 2)
132
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +0000133#define GB_LOOPBACK_US_WAIT_MAX 1000000
Alexandre Bailon355a7052015-03-31 09:51:59 +0200134
Alexandre Bailon355a7052015-03-31 09:51:59 +0200135/* interface sysfs attributes */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000136#define gb_loopback_ro_attr(field) \
137static ssize_t field##_show(struct device *dev, \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200138 struct device_attribute *attr, \
139 char *buf) \
140{ \
Axel Haslam079fa322015-12-11 11:49:45 +0100141 struct gb_loopback *gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000142 return sprintf(buf, "%u\n", gb->field); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200143} \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000144static DEVICE_ATTR_RO(field)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200145
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000146#define gb_loopback_ro_stats_attr(name, field, type) \
147static ssize_t name##_##field##_show(struct device *dev, \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200148 struct device_attribute *attr, \
149 char *buf) \
150{ \
Axel Haslam079fa322015-12-11 11:49:45 +0100151 struct gb_loopback *gb = dev_get_drvdata(dev); \
Alexandre Bailon1dc8d3d2016-03-11 17:23:37 +0100152 /* Report 0 for min and max if no transfer successed */ \
153 if (!gb->requests_completed) \
154 return sprintf(buf, "0\n"); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000155 return sprintf(buf, "%"#type"\n", gb->name.field); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200156} \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000157static DEVICE_ATTR_RO(name##_##field)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200158
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000159#define gb_loopback_ro_avg_attr(name) \
160static ssize_t name##_avg_show(struct device *dev, \
Alex Elder7a135a92015-08-03 12:57:18 -0500161 struct device_attribute *attr, \
162 char *buf) \
163{ \
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100164 struct gb_loopback_stats *stats; \
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100165 struct gb_loopback *gb; \
Alexandre Bailonfb37f132016-02-25 18:19:16 +0100166 u64 avg, rem; \
167 u32 count; \
Axel Haslam079fa322015-12-11 11:49:45 +0100168 gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000169 stats = &gb->name; \
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100170 count = stats->count ? stats->count : 1; \
Alexandre Bailon58a527a2016-03-08 16:37:37 +0100171 avg = stats->sum + count / 2000000; /* round closest */ \
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100172 rem = do_div(avg, count); \
Alexandre Bailon89ec14c2016-03-08 16:37:36 +0100173 rem *= 1000000; \
174 do_div(rem, count); \
Alexandre Bailonfb37f132016-02-25 18:19:16 +0100175 return sprintf(buf, "%llu.%06u\n", avg, (u32)rem); \
Alex Elder7a135a92015-08-03 12:57:18 -0500176} \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000177static DEVICE_ATTR_RO(name##_avg)
Alex Elder7a135a92015-08-03 12:57:18 -0500178
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000179#define gb_loopback_stats_attrs(field) \
180 gb_loopback_ro_stats_attr(field, min, u); \
181 gb_loopback_ro_stats_attr(field, max, u); \
182 gb_loopback_ro_avg_attr(field)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200183
184#define gb_loopback_attr(field, type) \
185static ssize_t field##_show(struct device *dev, \
186 struct device_attribute *attr, \
187 char *buf) \
188{ \
Axel Haslam079fa322015-12-11 11:49:45 +0100189 struct gb_loopback *gb = dev_get_drvdata(dev); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200190 return sprintf(buf, "%"#type"\n", gb->field); \
191} \
192static ssize_t field##_store(struct device *dev, \
193 struct device_attribute *attr, \
194 const char *buf, \
195 size_t len) \
196{ \
197 int ret; \
Axel Haslam079fa322015-12-11 11:49:45 +0100198 struct gb_loopback *gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000199 mutex_lock(&gb->mutex); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200200 ret = sscanf(buf, "%"#type, &gb->field); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200201 if (ret != 1) \
Bryan O'Donoghue85d678c2015-07-28 18:34:36 +0100202 len = -EINVAL; \
203 else \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000204 gb_loopback_check_attr(gb, bundle); \
205 mutex_unlock(&gb->mutex); \
Alexandre Bailon355a7052015-03-31 09:51:59 +0200206 return len; \
207} \
208static DEVICE_ATTR_RW(field)
209
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100210#define gb_dev_loopback_ro_attr(field, conn) \
211static ssize_t field##_show(struct device *dev, \
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100212 struct device_attribute *attr, \
213 char *buf) \
214{ \
Axel Haslam079fa322015-12-11 11:49:45 +0100215 struct gb_loopback *gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000216 return sprintf(buf, "%u\n", gb->field); \
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100217} \
218static DEVICE_ATTR_RO(field)
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100219
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100220#define gb_dev_loopback_rw_attr(field, type) \
221static ssize_t field##_show(struct device *dev, \
222 struct device_attribute *attr, \
223 char *buf) \
224{ \
Axel Haslam079fa322015-12-11 11:49:45 +0100225 struct gb_loopback *gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000226 return sprintf(buf, "%"#type"\n", gb->field); \
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100227} \
228static ssize_t field##_store(struct device *dev, \
229 struct device_attribute *attr, \
230 const char *buf, \
231 size_t len) \
232{ \
233 int ret; \
Axel Haslam079fa322015-12-11 11:49:45 +0100234 struct gb_loopback *gb = dev_get_drvdata(dev); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000235 mutex_lock(&gb->mutex); \
236 ret = sscanf(buf, "%"#type, &gb->field); \
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100237 if (ret != 1) \
238 len = -EINVAL; \
239 else \
Axel Haslam079fa322015-12-11 11:49:45 +0100240 gb_loopback_check_attr(gb); \
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000241 mutex_unlock(&gb->mutex); \
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100242 return len; \
243} \
244static DEVICE_ATTR_RW(field)
245
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000246static void gb_loopback_reset_stats(struct gb_loopback *gb);
Axel Haslam079fa322015-12-11 11:49:45 +0100247static void gb_loopback_check_attr(struct gb_loopback *gb)
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100248{
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +0000249 if (gb->us_wait > GB_LOOPBACK_US_WAIT_MAX)
250 gb->us_wait = GB_LOOPBACK_US_WAIT_MAX;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000251 if (gb->size > gb_dev.size_max)
252 gb->size = gb_dev.size_max;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000253 gb->requests_timedout = 0;
254 gb->requests_completed = 0;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000255 gb->iteration_count = 0;
Alexandre Bailon01480ba32016-03-08 17:40:16 +0100256 gb->send_count = 0;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000257 gb->error = 0;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100258
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000259 if (kfifo_depth < gb->iteration_max) {
Axel Haslam079fa322015-12-11 11:49:45 +0100260 dev_warn(gb->dev,
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000261 "cannot log bytes %u kfifo_depth %u\n",
262 gb->iteration_max, kfifo_depth);
Bryan O'Donoghuecb60f492015-07-28 18:34:39 +0100263 }
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000264 kfifo_reset_out(&gb->kfifo_lat);
265 kfifo_reset_out(&gb->kfifo_ts);
Bryan O'Donoghuecb60f492015-07-28 18:34:39 +0100266
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000267 switch (gb->type) {
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100268 case GB_LOOPBACK_TYPE_PING:
269 case GB_LOOPBACK_TYPE_TRANSFER:
270 case GB_LOOPBACK_TYPE_SINK:
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000271 gb->jiffy_timeout = usecs_to_jiffies(gb->timeout);
272 if (!gb->jiffy_timeout)
273 gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MIN;
274 else if (gb->jiffy_timeout > GB_LOOPBACK_TIMEOUT_MAX)
275 gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MAX;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000276 gb_loopback_reset_stats(gb);
277 wake_up(&gb->wq);
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100278 break;
279 default:
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000280 gb->type = 0;
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100281 break;
282 }
Alexandre Bailon355a7052015-03-31 09:51:59 +0200283}
284
285/* Time to send and receive one message */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000286gb_loopback_stats_attrs(latency);
Bryan O'Donoghue583cbf52015-07-21 23:50:10 +0100287/* Number of requests sent per second on this cport */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000288gb_loopback_stats_attrs(requests_per_second);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200289/* Quantity of data sent and received on this cport */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000290gb_loopback_stats_attrs(throughput);
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +0100291/* Latency across the UniPro link from APBridge's perspective */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000292gb_loopback_stats_attrs(apbridge_unipro_latency);
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +0100293/* Firmware induced overhead in the GPBridge */
Sandeep Patile54b1062016-05-19 08:52:39 -0700294gb_loopback_stats_attrs(gbphy_firmware_latency);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000295
Bryan O'Donoghuee140c752015-07-21 23:50:09 +0100296/* Number of errors encountered during loop */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000297gb_loopback_ro_attr(error);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000298/* Number of requests successfully completed async */
299gb_loopback_ro_attr(requests_completed);
300/* Number of requests timed out async */
301gb_loopback_ro_attr(requests_timedout);
302/* Timeout minimum in useconds */
303gb_loopback_ro_attr(timeout_min);
304/* Timeout minimum in useconds */
305gb_loopback_ro_attr(timeout_max);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200306
307/*
Bryan O'Donoghue799a3f02015-07-21 23:50:07 +0100308 * Type of loopback message to send based on protocol type definitions
Alexandre Bailon355a7052015-03-31 09:51:59 +0200309 * 0 => Don't send message
Bryan O'Donoghue799a3f02015-07-21 23:50:07 +0100310 * 2 => Send ping message continuously (message without payload)
Alex Elder006335a2015-08-03 12:57:10 -0500311 * 3 => Send transfer message continuously (message with payload,
Bryan O'Donoghue799a3f02015-07-21 23:50:07 +0100312 * payload returned in response)
313 * 4 => Send a sink message (message with payload, no payload in response)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200314 */
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100315gb_dev_loopback_rw_attr(type, d);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200316/* Size of transfer message payload: 0-4096 bytes */
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100317gb_dev_loopback_rw_attr(size, u);
Alex Elder48f19ee2015-05-11 21:16:36 -0500318/* Time to wait between two messages: 0-1000 ms */
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +0000319gb_dev_loopback_rw_attr(us_wait, d);
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100320/* Maximum iterations for a given operation: 1-(2^32-1), 0 implies infinite */
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100321gb_dev_loopback_rw_attr(iteration_max, u);
322/* The current index of the for (i = 0; i < iteration_max; i++) loop */
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +0100323gb_dev_loopback_ro_attr(iteration_count, false);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000324/* A flag to indicate synchronous or asynchronous operations */
325gb_dev_loopback_rw_attr(async, u);
326/* Timeout of an individual asynchronous request */
327gb_dev_loopback_rw_attr(timeout, u);
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +0000328/* Maximum number of in-flight operations before back-off */
329gb_dev_loopback_rw_attr(outstanding_operations_max, u);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200330
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000331static struct attribute *loopback_attrs[] = {
332 &dev_attr_latency_min.attr,
333 &dev_attr_latency_max.attr,
334 &dev_attr_latency_avg.attr,
335 &dev_attr_requests_per_second_min.attr,
336 &dev_attr_requests_per_second_max.attr,
337 &dev_attr_requests_per_second_avg.attr,
338 &dev_attr_throughput_min.attr,
339 &dev_attr_throughput_max.attr,
340 &dev_attr_throughput_avg.attr,
341 &dev_attr_apbridge_unipro_latency_min.attr,
342 &dev_attr_apbridge_unipro_latency_max.attr,
343 &dev_attr_apbridge_unipro_latency_avg.attr,
Sandeep Patile54b1062016-05-19 08:52:39 -0700344 &dev_attr_gbphy_firmware_latency_min.attr,
345 &dev_attr_gbphy_firmware_latency_max.attr,
346 &dev_attr_gbphy_firmware_latency_avg.attr,
Alexandre Bailon355a7052015-03-31 09:51:59 +0200347 &dev_attr_type.attr,
348 &dev_attr_size.attr,
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +0000349 &dev_attr_us_wait.attr,
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100350 &dev_attr_iteration_count.attr,
351 &dev_attr_iteration_max.attr,
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000352 &dev_attr_async.attr,
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000353 &dev_attr_error.attr,
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000354 &dev_attr_requests_completed.attr,
355 &dev_attr_requests_timedout.attr,
356 &dev_attr_timeout.attr,
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +0000357 &dev_attr_outstanding_operations_max.attr,
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000358 &dev_attr_timeout_min.attr,
359 &dev_attr_timeout_max.attr,
Alexandre Bailon355a7052015-03-31 09:51:59 +0200360 NULL,
361};
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000362ATTRIBUTE_GROUPS(loopback);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200363
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100364static void gb_loopback_calculate_stats(struct gb_loopback *gb, bool error);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000365
Bryan O'Donoghuebd416102015-08-17 00:55:05 +0100366static u32 gb_loopback_nsec_to_usec_latency(u64 elapsed_nsecs)
367{
368 u32 lat;
369
370 do_div(elapsed_nsecs, NSEC_PER_USEC);
371 lat = elapsed_nsecs;
372 return lat;
373}
374
Bryan O'Donoghue7c985352015-08-17 00:55:06 +0100375static u64 __gb_loopback_calc_latency(u64 t1, u64 t2)
376{
377 if (t2 > t1)
378 return t2 - t1;
379 else
380 return NSEC_PER_DAY - t2 + t1;
381}
382
Bryan O'Donoghue2f842302015-08-17 00:55:02 +0100383static u64 gb_loopback_calc_latency(struct timeval *ts, struct timeval *te)
Bryan O'Donoghue4c192662015-08-11 13:50:53 +0100384{
385 u64 t1, t2;
386
387 t1 = timeval_to_ns(ts);
388 t2 = timeval_to_ns(te);
Bryan O'Donoghue7c985352015-08-17 00:55:06 +0100389
390 return __gb_loopback_calc_latency(t1, t2);
Bryan O'Donoghue4c192662015-08-11 13:50:53 +0100391}
392
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +0100393static void gb_loopback_push_latency_ts(struct gb_loopback *gb,
394 struct timeval *ts, struct timeval *te)
395{
396 kfifo_in(&gb->kfifo_ts, (unsigned char *)ts, sizeof(*ts));
397 kfifo_in(&gb->kfifo_ts, (unsigned char *)te, sizeof(*te));
398}
399
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100400static int gb_loopback_operation_sync(struct gb_loopback *gb, int type,
401 void *request, int request_size,
402 void *response, int response_size)
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100403{
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100404 struct gb_operation *operation;
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100405 struct timeval ts, te;
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100406 int ret;
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100407
408 do_gettimeofday(&ts);
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100409 operation = gb_operation_create(gb->connection, type, request_size,
410 response_size, GFP_KERNEL);
Alexandre Bailond9048d82016-02-25 18:19:14 +0100411 if (!operation)
412 return -ENOMEM;
Bryan O'Donoghuec3bba872015-07-13 20:20:50 +0100413
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100414 if (request_size)
415 memcpy(operation->request->payload, request, request_size);
416
417 ret = gb_operation_request_send_sync(operation);
418 if (ret) {
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700419 dev_err(&gb->connection->bundle->dev,
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100420 "synchronous operation failed: %d\n", ret);
Alexandre Bailond9048d82016-02-25 18:19:14 +0100421 goto out_put_operation;
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100422 } else {
423 if (response_size == operation->response->payload_size) {
424 memcpy(response, operation->response->payload,
425 response_size);
426 } else {
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700427 dev_err(&gb->connection->bundle->dev,
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100428 "response size %zu expected %d\n",
429 operation->response->payload_size,
430 response_size);
Alexandre Bailond9048d82016-02-25 18:19:14 +0100431 ret = -EINVAL;
432 goto out_put_operation;
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100433 }
434 }
Johan Hovold6ab1ce42015-09-26 17:59:15 -0700435
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100436 do_gettimeofday(&te);
Bryan O'Donoghue2f842302015-08-17 00:55:02 +0100437
438 /* Calculate the total time the message took */
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +0100439 gb_loopback_push_latency_ts(gb, &ts, &te);
Bryan O'Donoghue2f842302015-08-17 00:55:02 +0100440 gb->elapsed_nsecs = gb_loopback_calc_latency(&ts, &te);
441
Alexandre Bailond9048d82016-02-25 18:19:14 +0100442out_put_operation:
443 gb_operation_put(operation);
444
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100445 return ret;
446}
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100447
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000448static void __gb_loopback_async_operation_destroy(struct kref *kref)
449{
450 struct gb_loopback_async_operation *op_async;
451
452 op_async = container_of(kref, struct gb_loopback_async_operation, kref);
453
454 list_del(&op_async->entry);
455 if (op_async->operation)
456 gb_operation_put(op_async->operation);
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +0000457 atomic_dec(&op_async->gb->outstanding_operations);
458 wake_up(&op_async->gb->wq_completion);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000459 kfree(op_async);
460}
461
462static void gb_loopback_async_operation_get(struct gb_loopback_async_operation
463 *op_async)
464{
465 kref_get(&op_async->kref);
466}
467
468static void gb_loopback_async_operation_put(struct gb_loopback_async_operation
469 *op_async)
470{
471 unsigned long flags;
472
473 spin_lock_irqsave(&gb_dev.lock, flags);
474 kref_put(&op_async->kref, __gb_loopback_async_operation_destroy);
475 spin_unlock_irqrestore(&gb_dev.lock, flags);
476}
477
478static struct gb_loopback_async_operation *
479 gb_loopback_operation_find(u16 id)
480{
481 struct gb_loopback_async_operation *op_async;
482 bool found = false;
483 unsigned long flags;
484
485 spin_lock_irqsave(&gb_dev.lock, flags);
486 list_for_each_entry(op_async, &gb_dev.list_op_async, entry) {
487 if (op_async->operation->id == id) {
488 gb_loopback_async_operation_get(op_async);
489 found = true;
490 break;
491 }
492 }
493 spin_unlock_irqrestore(&gb_dev.lock, flags);
494
495 return found ? op_async : NULL;
496}
497
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +0000498static void gb_loopback_async_wait_all(struct gb_loopback *gb)
499{
500 wait_event(gb->wq_completion,
501 !atomic_read(&gb->outstanding_operations));
502}
503
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000504static void gb_loopback_async_operation_callback(struct gb_operation *operation)
505{
506 struct gb_loopback_async_operation *op_async;
507 struct gb_loopback *gb;
508 struct timeval te;
509 bool err = false;
510
511 do_gettimeofday(&te);
512 op_async = gb_loopback_operation_find(operation->id);
513 if (!op_async)
514 return;
515
516 gb = op_async->gb;
517 mutex_lock(&gb->mutex);
518
519 if (!op_async->pending || gb_operation_result(operation)) {
520 err = true;
521 } else {
522 if (op_async->completion)
523 if (op_async->completion(op_async))
524 err = true;
525 }
526
Johan Hovold41993cd2016-03-03 15:29:39 +0100527 if (!err) {
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000528 gb_loopback_push_latency_ts(gb, &op_async->ts, &te);
529 gb->elapsed_nsecs = gb_loopback_calc_latency(&op_async->ts,
530 &te);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000531 }
532
533 if (op_async->pending) {
Johan Hovold41993cd2016-03-03 15:29:39 +0100534 if (err)
535 gb->error++;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000536 gb->iteration_count++;
537 op_async->pending = false;
538 del_timer_sync(&op_async->timer);
539 gb_loopback_async_operation_put(op_async);
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100540 gb_loopback_calculate_stats(gb, err);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000541 }
542 mutex_unlock(&gb->mutex);
543
544 dev_dbg(&gb->connection->bundle->dev, "complete operation %d\n",
545 operation->id);
546
547 gb_loopback_async_operation_put(op_async);
548}
549
550static void gb_loopback_async_operation_work(struct work_struct *work)
551{
552 struct gb_loopback *gb;
553 struct gb_operation *operation;
554 struct gb_loopback_async_operation *op_async;
555
556 op_async = container_of(work, struct gb_loopback_async_operation, work);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000557 gb = op_async->gb;
558 operation = op_async->operation;
559
560 mutex_lock(&gb->mutex);
561 if (op_async->pending) {
562 gb->requests_timedout++;
563 gb->error++;
564 gb->iteration_count++;
565 op_async->pending = false;
566 gb_loopback_async_operation_put(op_async);
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100567 gb_loopback_calculate_stats(gb, true);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000568 }
569 mutex_unlock(&gb->mutex);
570
571 dev_dbg(&gb->connection->bundle->dev, "timeout operation %d\n",
572 operation->id);
573
574 gb_operation_cancel(operation, -ETIMEDOUT);
575 gb_loopback_async_operation_put(op_async);
576}
577
578static void gb_loopback_async_operation_timeout(unsigned long data)
579{
580 struct gb_loopback_async_operation *op_async;
581 u16 id = data;
582
583 op_async = gb_loopback_operation_find(id);
584 if (!op_async) {
585 pr_err("operation %d not found - time out ?\n", id);
586 return;
587 }
588 schedule_work(&op_async->work);
589}
590
591static int gb_loopback_async_operation(struct gb_loopback *gb, int type,
592 void *request, int request_size,
593 int response_size,
594 void *completion)
595{
596 struct gb_loopback_async_operation *op_async;
597 struct gb_operation *operation;
598 int ret;
599 unsigned long flags;
600
601 op_async = kzalloc(sizeof(*op_async), GFP_KERNEL);
602 if (!op_async)
603 return -ENOMEM;
604
605 INIT_WORK(&op_async->work, gb_loopback_async_operation_work);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000606 kref_init(&op_async->kref);
607
608 operation = gb_operation_create(gb->connection, type, request_size,
609 response_size, GFP_KERNEL);
610 if (!operation) {
Bryan O'Donoghuec7aae4e2015-12-11 13:46:51 +0000611 kfree(op_async);
612 return -ENOMEM;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000613 }
614
615 if (request_size)
616 memcpy(operation->request->payload, request, request_size);
617
618 op_async->gb = gb;
619 op_async->operation = operation;
620 op_async->completion = completion;
621
622 spin_lock_irqsave(&gb_dev.lock, flags);
623 list_add_tail(&op_async->entry, &gb_dev.list_op_async);
624 spin_unlock_irqrestore(&gb_dev.lock, flags);
625
626 do_gettimeofday(&op_async->ts);
627 op_async->pending = true;
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +0000628 atomic_inc(&gb->outstanding_operations);
Bryan O'Donoghuefece9c82016-03-16 11:29:59 +0000629 mutex_lock(&gb->mutex);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000630 ret = gb_operation_request_send(operation,
631 gb_loopback_async_operation_callback,
632 GFP_KERNEL);
633 if (ret)
634 goto error;
635
sayli karnik82af03f2016-09-22 22:09:06 +0530636 setup_timer(&op_async->timer, gb_loopback_async_operation_timeout,
637 (unsigned long)operation->id);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000638 op_async->timer.expires = jiffies + gb->jiffy_timeout;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000639 add_timer(&op_async->timer);
640
Bryan O'Donoghuefece9c82016-03-16 11:29:59 +0000641 goto done;
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000642error:
643 gb_loopback_async_operation_put(op_async);
Bryan O'Donoghuefece9c82016-03-16 11:29:59 +0000644done:
645 mutex_unlock(&gb->mutex);
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000646 return ret;
647}
648
649static int gb_loopback_sync_sink(struct gb_loopback *gb, u32 len)
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100650{
651 struct gb_loopback_transfer_request *request;
652 int retval;
653
654 request = kmalloc(len + sizeof(*request), GFP_KERNEL);
655 if (!request)
656 return -ENOMEM;
657
658 request->len = cpu_to_le32(len);
659 retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_SINK,
660 request, len + sizeof(*request),
661 NULL, 0);
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100662 kfree(request);
663 return retval;
664}
665
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000666static int gb_loopback_sync_transfer(struct gb_loopback *gb, u32 len)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200667{
Alexandre Bailon355a7052015-03-31 09:51:59 +0200668 struct gb_loopback_transfer_request *request;
669 struct gb_loopback_transfer_response *response;
670 int retval;
671
Bryan O'Donoghued6a1a3b2015-12-03 17:29:39 +0000672 gb->apbridge_latency_ts = 0;
Sandeep Patile54b1062016-05-19 08:52:39 -0700673 gb->gbphy_latency_ts = 0;
Bryan O'Donoghued6a1a3b2015-12-03 17:29:39 +0000674
Alexandre Bailon355a7052015-03-31 09:51:59 +0200675 request = kmalloc(len + sizeof(*request), GFP_KERNEL);
676 if (!request)
677 return -ENOMEM;
678 response = kmalloc(len + sizeof(*response), GFP_KERNEL);
679 if (!response) {
680 kfree(request);
681 return -ENOMEM;
682 }
683
Viresh Kumardc4a1062015-08-14 17:07:10 +0530684 memset(request->data, 0x5A, len);
685
Alexandre Bailon355a7052015-03-31 09:51:59 +0200686 request->len = cpu_to_le32(len);
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100687 retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_TRANSFER,
688 request, len + sizeof(*request),
689 response, len + sizeof(*response));
Alexandre Bailon355a7052015-03-31 09:51:59 +0200690 if (retval)
691 goto gb_error;
692
Viresh Kumardc366f82015-08-14 17:07:11 +0530693 if (memcmp(request->data, response->data, len)) {
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700694 dev_err(&gb->connection->bundle->dev,
695 "Loopback Data doesn't match\n");
Alexandre Bailon355a7052015-03-31 09:51:59 +0200696 retval = -EREMOTEIO;
Viresh Kumardc366f82015-08-14 17:07:11 +0530697 }
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +0100698 gb->apbridge_latency_ts = (u32)__le32_to_cpu(response->reserved0);
Sandeep Patile54b1062016-05-19 08:52:39 -0700699 gb->gbphy_latency_ts = (u32)__le32_to_cpu(response->reserved1);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200700
701gb_error:
702 kfree(request);
703 kfree(response);
704
705 return retval;
706}
707
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000708static int gb_loopback_sync_ping(struct gb_loopback *gb)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200709{
Bryan O'Donoghuefbb8edb2015-09-14 10:48:47 +0100710 return gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_PING,
711 NULL, 0, NULL, 0);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200712}
713
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000714static int gb_loopback_async_sink(struct gb_loopback *gb, u32 len)
715{
716 struct gb_loopback_transfer_request *request;
717 int retval;
718
719 request = kmalloc(len + sizeof(*request), GFP_KERNEL);
720 if (!request)
721 return -ENOMEM;
722
723 request->len = cpu_to_le32(len);
724 retval = gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_SINK,
725 request, len + sizeof(*request),
726 0, NULL);
727 kfree(request);
728 return retval;
729}
730
731static int gb_loopback_async_transfer_complete(
732 struct gb_loopback_async_operation *op_async)
733{
734 struct gb_loopback *gb;
735 struct gb_operation *operation;
736 struct gb_loopback_transfer_request *request;
737 struct gb_loopback_transfer_response *response;
738 size_t len;
739 int retval = 0;
740
741 gb = op_async->gb;
742 operation = op_async->operation;
743 request = operation->request->payload;
744 response = operation->response->payload;
745 len = le32_to_cpu(request->len);
746
747 if (memcmp(request->data, response->data, len)) {
748 dev_err(&gb->connection->bundle->dev,
749 "Loopback Data doesn't match operation id %d\n",
750 operation->id);
751 retval = -EREMOTEIO;
752 } else {
753 gb->apbridge_latency_ts =
754 (u32)__le32_to_cpu(response->reserved0);
Sandeep Patile54b1062016-05-19 08:52:39 -0700755 gb->gbphy_latency_ts =
Bryan O'Donoghue12927832015-12-07 01:59:06 +0000756 (u32)__le32_to_cpu(response->reserved1);
757 }
758
759 return retval;
760}
761
762static int gb_loopback_async_transfer(struct gb_loopback *gb, u32 len)
763{
764 struct gb_loopback_transfer_request *request;
765 int retval, response_len;
766
767 request = kmalloc(len + sizeof(*request), GFP_KERNEL);
768 if (!request)
769 return -ENOMEM;
770
771 memset(request->data, 0x5A, len);
772
773 request->len = cpu_to_le32(len);
774 response_len = sizeof(struct gb_loopback_transfer_response);
775 retval = gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_TRANSFER,
776 request, len + sizeof(*request),
777 len + response_len,
778 gb_loopback_async_transfer_complete);
779 if (retval)
780 goto gb_error;
781
782gb_error:
783 kfree(request);
784 return retval;
785}
786
787static int gb_loopback_async_ping(struct gb_loopback *gb)
788{
789 return gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_PING,
790 NULL, 0, 0, NULL);
791}
792
Viresh Kumare82a11d2016-02-12 16:08:29 +0530793static int gb_loopback_request_handler(struct gb_operation *operation)
Alex Elderac1c2842015-05-11 21:16:39 -0500794{
795 struct gb_connection *connection = operation->connection;
796 struct gb_loopback_transfer_request *request;
797 struct gb_loopback_transfer_response *response;
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700798 struct device *dev = &connection->bundle->dev;
Bryan O'Donoghuee51eafe2015-07-14 00:53:13 +0100799 size_t len;
Alex Elderac1c2842015-05-11 21:16:39 -0500800
801 /* By convention, the AP initiates the version operation */
Viresh Kumare82a11d2016-02-12 16:08:29 +0530802 switch (operation->type) {
Alex Elderac1c2842015-05-11 21:16:39 -0500803 case GB_LOOPBACK_TYPE_PING:
Bryan O'Donoghue384a7a32015-07-13 20:20:49 +0100804 case GB_LOOPBACK_TYPE_SINK:
Alex Elderac1c2842015-05-11 21:16:39 -0500805 return 0;
806 case GB_LOOPBACK_TYPE_TRANSFER:
807 if (operation->request->payload_size < sizeof(*request)) {
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700808 dev_err(dev, "transfer request too small (%zu < %zu)\n",
Alex Elderac1c2842015-05-11 21:16:39 -0500809 operation->request->payload_size,
810 sizeof(*request));
811 return -EINVAL; /* -EMSGSIZE */
812 }
813 request = operation->request->payload;
814 len = le32_to_cpu(request->len);
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100815 if (len > gb_dev.size_max) {
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -0700816 dev_err(dev, "transfer request too large (%zu > %zu)\n",
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100817 len, gb_dev.size_max);
Bryan O'Donoghuec3bba872015-07-13 20:20:50 +0100818 return -EINVAL;
819 }
820
Bartosz Golaszewski81ad6992015-11-19 13:46:43 +0100821 if (!gb_operation_response_alloc(operation,
822 len + sizeof(*response), GFP_KERNEL)) {
823 dev_err(dev, "error allocating response\n");
824 return -ENOMEM;
Alex Elderac1c2842015-05-11 21:16:39 -0500825 }
Bartosz Golaszewski81ad6992015-11-19 13:46:43 +0100826 response = operation->response->payload;
827 response->len = cpu_to_le32(len);
828 if (len)
829 memcpy(response->data, request->data, len);
830
Alex Elderac1c2842015-05-11 21:16:39 -0500831 return 0;
832 default:
Viresh Kumare82a11d2016-02-12 16:08:29 +0530833 dev_err(dev, "unsupported request: %u\n", operation->type);
Alex Elderac1c2842015-05-11 21:16:39 -0500834 return -EINVAL;
835 }
836}
837
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000838static void gb_loopback_reset_stats(struct gb_loopback *gb)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200839{
840 struct gb_loopback_stats reset = {
Alex Eldere051c822015-08-03 12:57:14 -0500841 .min = U32_MAX,
Alexandre Bailon355a7052015-03-31 09:51:59 +0200842 };
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100843
844 /* Reset per-connection stats */
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000845 memcpy(&gb->latency, &reset,
846 sizeof(struct gb_loopback_stats));
847 memcpy(&gb->throughput, &reset,
848 sizeof(struct gb_loopback_stats));
849 memcpy(&gb->requests_per_second, &reset,
850 sizeof(struct gb_loopback_stats));
851 memcpy(&gb->apbridge_unipro_latency, &reset,
852 sizeof(struct gb_loopback_stats));
Sandeep Patile54b1062016-05-19 08:52:39 -0700853 memcpy(&gb->gbphy_firmware_latency, &reset,
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000854 sizeof(struct gb_loopback_stats));
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100855
Bryan O'Donoghuef42a6892015-12-07 01:59:08 +0000856 /* Should be initialized at least once per transaction set */
857 gb->apbridge_latency_ts = 0;
Sandeep Patile54b1062016-05-19 08:52:39 -0700858 gb->gbphy_latency_ts = 0;
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100859 memset(&gb->ts, 0, sizeof(struct timeval));
Alexandre Bailon355a7052015-03-31 09:51:59 +0200860}
861
Alex Eldera6e7e532015-08-03 12:57:13 -0500862static void gb_loopback_update_stats(struct gb_loopback_stats *stats, u32 val)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200863{
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100864 if (stats->min > val)
865 stats->min = val;
866 if (stats->max < val)
867 stats->max = val;
868 stats->sum += val;
869 stats->count++;
Alexandre Bailon355a7052015-03-31 09:51:59 +0200870}
871
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100872static void gb_loopback_update_stats_window(struct gb_loopback_stats *stats,
873 u64 val, u32 count)
874{
875 stats->sum += val;
876 stats->count += count;
877
878 do_div(val, count);
879 if (stats->min > val)
880 stats->min = val;
881 if (stats->max < val)
882 stats->max = val;
883}
884
Bryan O'Donoghue583cbf52015-07-21 23:50:10 +0100885static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200886{
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100887 u64 req = gb->requests_completed * USEC_PER_SEC;
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100888
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100889 gb_loopback_update_stats_window(&gb->requests_per_second, req, latency);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200890}
891
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100892static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200893{
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100894 u64 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2;
Bryan O'Donoghuef7908e42015-07-13 20:20:51 +0100895
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000896 switch (gb->type) {
Bryan O'Donoghuef7908e42015-07-13 20:20:51 +0100897 case GB_LOOPBACK_TYPE_PING:
898 break;
899 case GB_LOOPBACK_TYPE_SINK:
900 aggregate_size += sizeof(struct gb_loopback_transfer_request) +
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000901 gb->size;
Bryan O'Donoghuef7908e42015-07-13 20:20:51 +0100902 break;
903 case GB_LOOPBACK_TYPE_TRANSFER:
904 aggregate_size += sizeof(struct gb_loopback_transfer_request) +
905 sizeof(struct gb_loopback_transfer_response) +
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000906 gb->size * 2;
Bryan O'Donoghuef7908e42015-07-13 20:20:51 +0100907 break;
908 default:
909 return;
910 }
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100911
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100912 aggregate_size *= gb->requests_completed;
913 aggregate_size *= USEC_PER_SEC;
914 gb_loopback_update_stats_window(&gb->throughput, aggregate_size,
915 latency);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200916}
917
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100918static void gb_loopback_calculate_latency_stats(struct gb_loopback *gb)
Alexandre Bailon355a7052015-03-31 09:51:59 +0200919{
920 u32 lat;
Alexandre Bailon355a7052015-03-31 09:51:59 +0200921
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100922 /* Express latency in terms of microseconds */
Bryan O'Donoghuebd416102015-08-17 00:55:05 +0100923 lat = gb_loopback_nsec_to_usec_latency(gb->elapsed_nsecs);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200924
Bryan O'Donoghue98676ca2015-08-17 00:55:12 +0100925 /* Log latency stastic */
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100926 gb_loopback_update_stats(&gb->latency, lat);
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100927
928 /* Raw latency log on a per thread basis */
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +0100929 kfifo_in(&gb->kfifo_lat, (unsigned char *)&lat, sizeof(lat));
Bryan O'Donoghue00af6582015-07-21 23:50:08 +0100930
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +0100931 /* Log the firmware supplied latency values */
Bryan O'Donoghue1ec58432015-10-15 16:10:45 +0100932 gb_loopback_update_stats(&gb->apbridge_unipro_latency,
933 gb->apbridge_latency_ts);
Sandeep Patile54b1062016-05-19 08:52:39 -0700934 gb_loopback_update_stats(&gb->gbphy_firmware_latency,
935 gb->gbphy_latency_ts);
Alexandre Bailon355a7052015-03-31 09:51:59 +0200936}
937
Alexandre Bailonab81bb92016-02-25 18:19:15 +0100938static void gb_loopback_calculate_stats(struct gb_loopback *gb, bool error)
939{
940 u64 nlat;
941 u32 lat;
942 struct timeval te;
943
944 if (!error) {
945 gb->requests_completed++;
946 gb_loopback_calculate_latency_stats(gb);
947 }
948
949 do_gettimeofday(&te);
950 nlat = gb_loopback_calc_latency(&gb->ts, &te);
951 if (nlat >= NSEC_PER_SEC || gb->iteration_count == gb->iteration_max) {
952 lat = gb_loopback_nsec_to_usec_latency(nlat);
953
954 gb_loopback_throughput_update(gb, lat);
955 gb_loopback_requests_update(gb, lat);
956
957 if (gb->iteration_count != gb->iteration_max) {
958 gb->ts = te;
959 gb->requests_completed = 0;
960 }
961 }
962}
963
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +0000964static void gb_loopback_async_wait_to_send(struct gb_loopback *gb)
965{
966 if (!(gb->async && gb->outstanding_operations_max))
967 return;
968 wait_event_interruptible(gb->wq_completion,
969 (atomic_read(&gb->outstanding_operations) <
970 gb->outstanding_operations_max) ||
971 kthread_should_stop());
972}
973
Alexandre Bailon355a7052015-03-31 09:51:59 +0200974static int gb_loopback_fn(void *data)
975{
976 int error = 0;
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +0000977 int us_wait = 0;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100978 int type;
Axel Haslame854ff52016-07-14 15:13:00 -0500979 int ret;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +0100980 u32 size;
Alexandre Bailon01480ba32016-03-08 17:40:16 +0100981
Alex Elder3f12c3e2015-08-03 12:57:11 -0500982 struct gb_loopback *gb = data;
Axel Haslame854ff52016-07-14 15:13:00 -0500983 struct gb_bundle *bundle = gb->connection->bundle;
984
985 ret = gb_pm_runtime_get_sync(bundle);
986 if (ret)
987 return ret;
Alexandre Bailon355a7052015-03-31 09:51:59 +0200988
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100989 while (1) {
Axel Haslame854ff52016-07-14 15:13:00 -0500990 if (!gb->type) {
991 gb_pm_runtime_put_autosuspend(bundle);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +0000992 wait_event_interruptible(gb->wq, gb->type ||
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +0100993 kthread_should_stop());
Axel Haslame854ff52016-07-14 15:13:00 -0500994 ret = gb_pm_runtime_get_sync(bundle);
995 if (ret)
996 return ret;
997 }
998
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +0000999 if (kthread_should_stop())
1000 break;
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001001
Bryan O'Donoghue8e3fba52015-12-11 13:46:53 +00001002 /* Limit the maximum number of in-flight async operations */
1003 gb_loopback_async_wait_to_send(gb);
Bryan O'Donoghue3dfe8aa2015-07-24 10:02:56 +01001004 if (kthread_should_stop())
1005 break;
Bryan O'Donoghue85d678c2015-07-28 18:34:36 +01001006
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001007 mutex_lock(&gb->mutex);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001008
1009 /* Optionally terminate */
Alexandre Bailon01480ba32016-03-08 17:40:16 +01001010 if (gb->send_count == gb->iteration_max) {
Alexandre Bailonab81bb92016-02-25 18:19:15 +01001011 if (gb->iteration_count == gb->iteration_max) {
1012 gb->type = 0;
Alexandre Bailon01480ba32016-03-08 17:40:16 +01001013 gb->send_count = 0;
Axel Haslam39c27872016-02-26 11:39:48 +01001014 sysfs_notify(&gb->dev->kobj, NULL,
1015 "iteration_count");
Alexandre Bailonab81bb92016-02-25 18:19:15 +01001016 }
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001017 mutex_unlock(&gb->mutex);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001018 continue;
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001019 }
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001020 size = gb->size;
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +00001021 us_wait = gb->us_wait;
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001022 type = gb->type;
Alexandre Bailonab81bb92016-02-25 18:19:15 +01001023 if (gb->ts.tv_usec == 0 && gb->ts.tv_sec == 0)
1024 do_gettimeofday(&gb->ts);
Bryan O'Donoghued9fb3752015-12-03 17:29:38 +00001025 mutex_unlock(&gb->mutex);
1026
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001027 /* Else operations to perform */
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001028 if (gb->async) {
1029 if (type == GB_LOOPBACK_TYPE_PING) {
1030 error = gb_loopback_async_ping(gb);
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001031 } else if (type == GB_LOOPBACK_TYPE_TRANSFER) {
1032 error = gb_loopback_async_transfer(gb, size);
1033 } else if (type == GB_LOOPBACK_TYPE_SINK) {
1034 error = gb_loopback_async_sink(gb, size);
1035 }
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001036
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001037 if (error)
1038 gb->error++;
1039 } else {
1040 /* We are effectively single threaded here */
1041 if (type == GB_LOOPBACK_TYPE_PING)
1042 error = gb_loopback_sync_ping(gb);
1043 else if (type == GB_LOOPBACK_TYPE_TRANSFER)
1044 error = gb_loopback_sync_transfer(gb, size);
1045 else if (type == GB_LOOPBACK_TYPE_SINK)
1046 error = gb_loopback_sync_sink(gb, size);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001047
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001048 if (error)
1049 gb->error++;
1050 gb->iteration_count++;
Alexandre Bailonab81bb92016-02-25 18:19:15 +01001051 gb_loopback_calculate_stats(gb, !!error);
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001052 }
Alexandre Bailon01480ba32016-03-08 17:40:16 +01001053 gb->send_count++;
Bryan O'Donoghueb36f04f2015-12-07 01:59:07 +00001054 if (us_wait)
1055 udelay(us_wait);
Alexandre Bailon355a7052015-03-31 09:51:59 +02001056 }
Axel Haslame854ff52016-07-14 15:13:00 -05001057
1058 gb_pm_runtime_put_autosuspend(bundle);
1059
Alexandre Bailon355a7052015-03-31 09:51:59 +02001060 return 0;
1061}
1062
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001063static int gb_loopback_dbgfs_latency_show_common(struct seq_file *s,
1064 struct kfifo *kfifo,
1065 struct mutex *mutex)
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001066{
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001067 u32 latency;
1068 int retval;
1069
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001070 if (kfifo_len(kfifo) == 0) {
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001071 retval = -EAGAIN;
1072 goto done;
1073 }
1074
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001075 mutex_lock(mutex);
1076 retval = kfifo_out(kfifo, &latency, sizeof(latency));
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001077 if (retval > 0) {
1078 seq_printf(s, "%u", latency);
1079 retval = 0;
1080 }
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001081 mutex_unlock(mutex);
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001082done:
1083 return retval;
1084}
1085
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001086static int gb_loopback_dbgfs_latency_show(struct seq_file *s, void *unused)
1087{
1088 struct gb_loopback *gb = s->private;
1089
1090 return gb_loopback_dbgfs_latency_show_common(s, &gb->kfifo_lat,
1091 &gb->mutex);
1092}
1093
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001094static int gb_loopback_latency_open(struct inode *inode, struct file *file)
1095{
1096 return single_open(file, gb_loopback_dbgfs_latency_show,
1097 inode->i_private);
1098}
1099
1100static const struct file_operations gb_loopback_debugfs_latency_ops = {
1101 .open = gb_loopback_latency_open,
1102 .read = seq_read,
1103 .llseek = seq_lseek,
1104 .release = single_release,
1105};
1106
Bryan O'Donoghue50151152015-09-14 10:48:46 +01001107static int gb_loopback_bus_id_compare(void *priv, struct list_head *lha,
1108 struct list_head *lhb)
1109{
1110 struct gb_loopback *a = list_entry(lha, struct gb_loopback, entry);
1111 struct gb_loopback *b = list_entry(lhb, struct gb_loopback, entry);
1112 struct gb_connection *ca = a->connection;
1113 struct gb_connection *cb = b->connection;
1114
Bryan O'Donoghue50151152015-09-14 10:48:46 +01001115 if (ca->bundle->intf->interface_id < cb->bundle->intf->interface_id)
1116 return -1;
1117 if (cb->bundle->intf->interface_id < ca->bundle->intf->interface_id)
1118 return 1;
1119 if (ca->bundle->id < cb->bundle->id)
1120 return -1;
1121 if (cb->bundle->id < ca->bundle->id)
1122 return 1;
1123 if (ca->intf_cport_id < cb->intf_cport_id)
1124 return -1;
1125 else if (cb->intf_cport_id < ca->intf_cport_id)
1126 return 1;
1127
1128 return 0;
1129}
1130
1131static void gb_loopback_insert_id(struct gb_loopback *gb)
1132{
1133 struct gb_loopback *gb_list;
1134 u32 new_lbid = 0;
1135
1136 /* perform an insertion sort */
1137 list_add_tail(&gb->entry, &gb_dev.list);
1138 list_sort(NULL, &gb_dev.list, gb_loopback_bus_id_compare);
1139 list_for_each_entry(gb_list, &gb_dev.list, entry) {
1140 gb_list->lbid = 1 << new_lbid;
1141 new_lbid++;
1142 }
1143}
1144
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001145#define DEBUGFS_NAMELEN 32
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001146
Viresh Kumare82a11d2016-02-12 16:08:29 +05301147static int gb_loopback_probe(struct gb_bundle *bundle,
1148 const struct greybus_bundle_id *id)
Alexandre Bailon355a7052015-03-31 09:51:59 +02001149{
Viresh Kumare82a11d2016-02-12 16:08:29 +05301150 struct greybus_descriptor_cport *cport_desc;
1151 struct gb_connection *connection;
Alexandre Bailon355a7052015-03-31 09:51:59 +02001152 struct gb_loopback *gb;
Axel Haslam079fa322015-12-11 11:49:45 +01001153 struct device *dev;
Alexandre Bailon355a7052015-03-31 09:51:59 +02001154 int retval;
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001155 char name[DEBUGFS_NAMELEN];
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001156 unsigned long flags;
Alexandre Bailon355a7052015-03-31 09:51:59 +02001157
Viresh Kumare82a11d2016-02-12 16:08:29 +05301158 if (bundle->num_cports != 1)
1159 return -ENODEV;
1160
1161 cport_desc = &bundle->cport_desc[0];
1162 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LOOPBACK)
1163 return -ENODEV;
1164
Alexandre Bailon355a7052015-03-31 09:51:59 +02001165 gb = kzalloc(sizeof(*gb), GFP_KERNEL);
1166 if (!gb)
1167 return -ENOMEM;
1168
Viresh Kumare82a11d2016-02-12 16:08:29 +05301169 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1170 gb_loopback_request_handler);
1171 if (IS_ERR(connection)) {
1172 retval = PTR_ERR(connection);
1173 goto out_kzalloc;
1174 }
1175
1176 gb->connection = connection;
1177 greybus_set_drvdata(bundle, gb);
1178
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001179 init_waitqueue_head(&gb->wq);
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +00001180 init_waitqueue_head(&gb->wq_completion);
1181 atomic_set(&gb->outstanding_operations, 0);
Bryan O'Donoghue8e1d6c32015-12-03 17:29:41 +00001182 gb_loopback_reset_stats(gb);
1183
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001184 /* Reported values to user-space for min/max timeouts */
1185 gb->timeout_min = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MIN);
1186 gb->timeout_max = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MAX);
1187
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +01001188 if (!gb_dev.count) {
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +01001189 /* Calculate maximum payload */
1190 gb_dev.size_max = gb_operation_get_payload_size_max(connection);
1191 if (gb_dev.size_max <=
1192 sizeof(struct gb_loopback_transfer_request)) {
1193 retval = -EINVAL;
Viresh Kumare82a11d2016-02-12 16:08:29 +05301194 goto out_connection_destroy;
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +01001195 }
1196 gb_dev.size_max -= sizeof(struct gb_loopback_transfer_request);
1197 }
Bryan O'Donoghuef06272b2015-08-17 00:55:11 +01001198
1199 /* Create per-connection sysfs and debugfs data-points */
Bryan O'Donoghue8d8d36d2015-09-23 09:31:33 -07001200 snprintf(name, sizeof(name), "raw_latency_%s",
Greg Kroah-Hartmand9a9ea12015-10-16 16:55:46 -07001201 dev_name(&connection->bundle->dev));
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001202 gb->file = debugfs_create_file(name, S_IFREG | S_IRUGO, gb_dev.root, gb,
1203 &gb_loopback_debugfs_latency_ops);
Axel Haslam079fa322015-12-11 11:49:45 +01001204
1205 gb->id = ida_simple_get(&loopback_ida, 0, 0, GFP_KERNEL);
1206 if (gb->id < 0) {
1207 retval = gb->id;
Viresh Kumare82a11d2016-02-12 16:08:29 +05301208 goto out_debugfs_remove;
Axel Haslam079fa322015-12-11 11:49:45 +01001209 }
1210
Viresh Kumare82a11d2016-02-12 16:08:29 +05301211 retval = gb_connection_enable(connection);
1212 if (retval)
1213 goto out_ida_remove;
1214
Axel Haslam079fa322015-12-11 11:49:45 +01001215 dev = device_create_with_groups(&loopback_class,
1216 &connection->bundle->dev,
1217 MKDEV(0, 0), gb, loopback_groups,
1218 "gb_loopback%d", gb->id);
1219 if (IS_ERR(dev)) {
1220 retval = PTR_ERR(dev);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301221 goto out_connection_disable;
Axel Haslam079fa322015-12-11 11:49:45 +01001222 }
1223 gb->dev = dev;
Bryan O'Donoghuec3bba872015-07-13 20:20:50 +01001224
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001225 /* Allocate kfifo */
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001226 if (kfifo_alloc(&gb->kfifo_lat, kfifo_depth * sizeof(u32),
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001227 GFP_KERNEL)) {
1228 retval = -ENOMEM;
Axel Haslam079fa322015-12-11 11:49:45 +01001229 goto out_conn;
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001230 }
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001231 if (kfifo_alloc(&gb->kfifo_ts, kfifo_depth * sizeof(struct timeval) * 2,
1232 GFP_KERNEL)) {
1233 retval = -ENOMEM;
1234 goto out_kfifo0;
1235 }
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001236
1237 /* Fork worker thread */
Bryan O'Donoghue85d678c2015-07-28 18:34:36 +01001238 mutex_init(&gb->mutex);
Alexandre Bailon355a7052015-03-31 09:51:59 +02001239 gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback");
1240 if (IS_ERR(gb->task)) {
Alex Elder69f60342015-05-11 21:16:35 -05001241 retval = PTR_ERR(gb->task);
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001242 goto out_kfifo1;
Alexandre Bailon355a7052015-03-31 09:51:59 +02001243 }
1244
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001245 spin_lock_irqsave(&gb_dev.lock, flags);
Bryan O'Donoghue50151152015-09-14 10:48:46 +01001246 gb_loopback_insert_id(gb);
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001247 gb_dev.count++;
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001248 spin_unlock_irqrestore(&gb_dev.lock, flags);
1249
1250 gb_connection_latency_tag_enable(connection);
Axel Haslame854ff52016-07-14 15:13:00 -05001251
1252 gb_pm_runtime_put_autosuspend(bundle);
1253
Alexandre Bailon355a7052015-03-31 09:51:59 +02001254 return 0;
1255
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001256out_kfifo1:
1257 kfifo_free(&gb->kfifo_ts);
1258out_kfifo0:
1259 kfifo_free(&gb->kfifo_lat);
Axel Haslam079fa322015-12-11 11:49:45 +01001260out_conn:
1261 device_unregister(dev);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301262out_connection_disable:
1263 gb_connection_disable(connection);
1264out_ida_remove:
Axel Haslam079fa322015-12-11 11:49:45 +01001265 ida_simple_remove(&loopback_ida, gb->id);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301266out_debugfs_remove:
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001267 debugfs_remove(gb->file);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301268out_connection_destroy:
1269 gb_connection_destroy(connection);
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001270out_kzalloc:
Alexandre Bailon355a7052015-03-31 09:51:59 +02001271 kfree(gb);
Alex Elder1fb807c2015-08-03 12:57:20 -05001272
Alexandre Bailon355a7052015-03-31 09:51:59 +02001273 return retval;
1274}
1275
Viresh Kumare82a11d2016-02-12 16:08:29 +05301276static void gb_loopback_disconnect(struct gb_bundle *bundle)
Alexandre Bailon355a7052015-03-31 09:51:59 +02001277{
Viresh Kumare82a11d2016-02-12 16:08:29 +05301278 struct gb_loopback *gb = greybus_get_drvdata(bundle);
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001279 unsigned long flags;
Axel Haslame854ff52016-07-14 15:13:00 -05001280 int ret;
1281
1282 ret = gb_pm_runtime_get_sync(bundle);
1283 if (ret)
1284 gb_pm_runtime_get_noresume(bundle);
Alexandre Bailon355a7052015-03-31 09:51:59 +02001285
Viresh Kumare82a11d2016-02-12 16:08:29 +05301286 gb_connection_disable(gb->connection);
1287
Alexandre Bailon355a7052015-03-31 09:51:59 +02001288 if (!IS_ERR_OR_NULL(gb->task))
1289 kthread_stop(gb->task);
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001290
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001291 kfifo_free(&gb->kfifo_lat);
1292 kfifo_free(&gb->kfifo_ts);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301293 gb_connection_latency_tag_disable(gb->connection);
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001294 debugfs_remove(gb->file);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301295
1296 /*
1297 * FIXME: gb_loopback_async_wait_all() is redundant now, as connection
1298 * is disabled at the beginning and so we can't have any more
1299 * incoming/outgoing requests.
1300 */
Bryan O'Donoghue36f241ff2015-12-11 13:46:52 +00001301 gb_loopback_async_wait_all(gb);
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001302
1303 spin_lock_irqsave(&gb_dev.lock, flags);
1304 gb_dev.count--;
Bryan O'Donoghueff477d02015-09-04 16:53:31 +01001305 list_del(&gb->entry);
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001306 spin_unlock_irqrestore(&gb_dev.lock, flags);
1307
Axel Haslam079fa322015-12-11 11:49:45 +01001308 device_unregister(gb->dev);
1309 ida_simple_remove(&loopback_ida, gb->id);
1310
Viresh Kumare82a11d2016-02-12 16:08:29 +05301311 gb_connection_destroy(gb->connection);
Bryan O'Donoghue5f3e0d12015-09-14 10:48:41 +01001312 kfree(gb);
Alexandre Bailon355a7052015-03-31 09:51:59 +02001313}
1314
Viresh Kumare82a11d2016-02-12 16:08:29 +05301315static const struct greybus_bundle_id gb_loopback_id_table[] = {
1316 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOOPBACK) },
1317 { }
1318};
1319MODULE_DEVICE_TABLE(greybus, gb_loopback_id_table);
1320
1321static struct greybus_driver gb_loopback_driver = {
1322 .name = "loopback",
1323 .probe = gb_loopback_probe,
1324 .disconnect = gb_loopback_disconnect,
1325 .id_table = gb_loopback_id_table,
Alexandre Bailon355a7052015-03-31 09:51:59 +02001326};
1327
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001328static int loopback_init(void)
1329{
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001330 int retval;
1331
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001332 INIT_LIST_HEAD(&gb_dev.list);
Bryan O'Donoghue12927832015-12-07 01:59:06 +00001333 INIT_LIST_HEAD(&gb_dev.list_op_async);
Bryan O'Donoghue2e238d712015-12-07 01:59:05 +00001334 spin_lock_init(&gb_dev.lock);
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001335 gb_dev.root = debugfs_create_dir("gb_loopback", NULL);
Bryan O'Donoghue67d1eec2015-08-17 00:55:04 +01001336
Axel Haslam079fa322015-12-11 11:49:45 +01001337 retval = class_register(&loopback_class);
1338 if (retval)
1339 goto err;
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001340
Viresh Kumare82a11d2016-02-12 16:08:29 +05301341 retval = greybus_register(&gb_loopback_driver);
Axel Haslam079fa322015-12-11 11:49:45 +01001342 if (retval)
1343 goto err_unregister;
1344
1345 return 0;
1346
1347err_unregister:
1348 class_unregister(&loopback_class);
1349err:
Bryan O'Donoghue4b0ea00c2015-08-17 00:55:07 +01001350 debugfs_remove_recursive(gb_dev.root);
1351 return retval;
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001352}
1353module_init(loopback_init);
1354
1355static void __exit loopback_exit(void)
1356{
Bryan O'Donoghueaa27bf82015-08-17 00:55:03 +01001357 debugfs_remove_recursive(gb_dev.root);
Viresh Kumare82a11d2016-02-12 16:08:29 +05301358 greybus_deregister(&gb_loopback_driver);
Axel Haslam079fa322015-12-11 11:49:45 +01001359 class_unregister(&loopback_class);
1360 ida_destroy(&loopback_ida);
Bryan O'Donoghuecbd204b2015-07-28 18:34:38 +01001361}
1362module_exit(loopback_exit);
Alexandre Bailon355a7052015-03-31 09:51:59 +02001363
1364MODULE_LICENSE("GPL v2");