greybus: spi: Add runtime_pm support
[linux-2.6-block.git] / drivers / staging / greybus / loopback.c
CommitLineData
355a7058
AB
1/*
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 */
8923c5b5
JH
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
355a7058
AB
12#include <linux/kernel.h>
13#include <linux/module.h>
85d678c0 14#include <linux/mutex.h>
355a7058
AB
15#include <linux/slab.h>
16#include <linux/kthread.h>
17#include <linux/delay.h>
18#include <linux/random.h>
5679f783 19#include <linux/sizes.h>
cbd204b4
BD
20#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/kfifo.h>
aa27bf82 23#include <linux/debugfs.h>
5015115d 24#include <linux/list_sort.h>
2e238d71 25#include <linux/spinlock.h>
12927835 26#include <linux/workqueue.h>
36f241ff 27#include <linux/atomic.h>
cbd204b4 28
1ffc12be
JH
29#include <asm/div64.h>
30
355a7058 31#include "greybus.h"
fd58926e 32#include "connection.h"
355a7058 33
fd489e1a
BD
34#define NSEC_PER_DAY 86400000000000ULL
35
355a7058 36struct gb_loopback_stats {
e051c82b
AE
37 u32 min;
38 u32 max;
00af6583 39 u64 sum;
e13fc28f 40 u32 count;
355a7058
AB
41};
42
aa27bf82
BD
43struct gb_loopback_device {
44 struct dentry *root;
45 u32 count;
8e1d6c33 46 size_t size_max;
67d1eece 47
2e238d71
BD
48 /* We need to take a lock in atomic context */
49 spinlock_t lock;
67d1eece 50 struct list_head list;
12927835
BD
51 struct list_head list_op_async;
52 wait_queue_head_t wq;
aa27bf82
BD
53};
54
55static struct gb_loopback_device gb_dev;
56
12927835
BD
57struct gb_loopback_async_operation {
58 struct gb_loopback *gb;
59 struct gb_operation *operation;
60 struct timeval ts;
61 struct timer_list timer;
62 struct list_head entry;
63 struct work_struct work;
64 struct kref kref;
65 bool pending;
66 int (*completion)(struct gb_loopback_async_operation *op_async);
67};
68
355a7058
AB
69struct gb_loopback {
70 struct gb_connection *connection;
355a7058 71
aa27bf82 72 struct dentry *file;
4b0ea00c
BD
73 struct kfifo kfifo_lat;
74 struct kfifo kfifo_ts;
85d678c0 75 struct mutex mutex;
355a7058 76 struct task_struct *task;
67d1eece 77 struct list_head entry;
079fa32b 78 struct device *dev;
8e1d6c33 79 wait_queue_head_t wq;
36f241ff
BD
80 wait_queue_head_t wq_completion;
81 atomic_t outstanding_operations;
355a7058 82
67d1eece 83 /* Per connection stats */
ab81bb9c 84 struct timeval ts;
355a7058
AB
85 struct gb_loopback_stats latency;
86 struct gb_loopback_stats throughput;
583cbf50 87 struct gb_loopback_stats requests_per_second;
1ec5843e 88 struct gb_loopback_stats apbridge_unipro_latency;
e54b106d 89 struct gb_loopback_stats gbphy_firmware_latency;
67d1eece 90
8e1d6c33 91 int type;
12927835 92 int async;
079fa32b 93 int id;
8e1d6c33
BD
94 u32 size;
95 u32 iteration_max;
67d1eece 96 u32 iteration_count;
b36f04fa 97 int us_wait;
355a7058 98 u32 error;
12927835
BD
99 u32 requests_completed;
100 u32 requests_timedout;
101 u32 timeout;
102 u32 jiffy_timeout;
103 u32 timeout_min;
104 u32 timeout_max;
8e3fba55 105 u32 outstanding_operations_max;
8e1d6c33
BD
106 u32 lbid;
107 u64 elapsed_nsecs;
e6227ee6 108 u32 apbridge_latency_ts;
e54b106d 109 u32 gbphy_latency_ts;
01480ba3
AB
110
111 u32 send_count;
355a7058
AB
112};
113
079fa32b
AH
114static struct class loopback_class = {
115 .name = "gb_loopback",
116 .owner = THIS_MODULE,
117};
118static DEFINE_IDA(loopback_ida);
119
12927835
BD
120/* Min/max values in jiffies */
121#define GB_LOOPBACK_TIMEOUT_MIN 1
122#define GB_LOOPBACK_TIMEOUT_MAX 10000
123
cbd204b4
BD
124#define GB_LOOPBACK_FIFO_DEFAULT 8192
125
cbd204b4
BD
126static unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT;
127module_param(kfifo_depth, uint, 0444);
128
cbd204b4
BD
129/* Maximum size of any one send data buffer we support */
130#define MAX_PACKET_SIZE (PAGE_SIZE * 2)
131
b36f04fa 132#define GB_LOOPBACK_US_WAIT_MAX 1000000
355a7058 133
355a7058 134/* interface sysfs attributes */
8e1d6c33
BD
135#define gb_loopback_ro_attr(field) \
136static ssize_t field##_show(struct device *dev, \
355a7058
AB
137 struct device_attribute *attr, \
138 char *buf) \
139{ \
079fa32b 140 struct gb_loopback *gb = dev_get_drvdata(dev); \
8e1d6c33 141 return sprintf(buf, "%u\n", gb->field); \
355a7058 142} \
8e1d6c33 143static DEVICE_ATTR_RO(field)
355a7058 144
8e1d6c33
BD
145#define gb_loopback_ro_stats_attr(name, field, type) \
146static ssize_t name##_##field##_show(struct device *dev, \
355a7058
AB
147 struct device_attribute *attr, \
148 char *buf) \
149{ \
079fa32b 150 struct gb_loopback *gb = dev_get_drvdata(dev); \
1dc8d3d7
AB
151 /* Report 0 for min and max if no transfer successed */ \
152 if (!gb->requests_completed) \
153 return sprintf(buf, "0\n"); \
8e1d6c33 154 return sprintf(buf, "%"#type"\n", gb->name.field); \
355a7058 155} \
8e1d6c33 156static DEVICE_ATTR_RO(name##_##field)
355a7058 157
8e1d6c33
BD
158#define gb_loopback_ro_avg_attr(name) \
159static ssize_t name##_avg_show(struct device *dev, \
7a135a96
AE
160 struct device_attribute *attr, \
161 char *buf) \
162{ \
f06272b2 163 struct gb_loopback_stats *stats; \
f06272b2 164 struct gb_loopback *gb; \
fb37f137
AB
165 u64 avg, rem; \
166 u32 count; \
079fa32b 167 gb = dev_get_drvdata(dev); \
8e1d6c33 168 stats = &gb->name; \
f06272b2 169 count = stats->count ? stats->count : 1; \
58a527af 170 avg = stats->sum + count / 2000000; /* round closest */ \
f06272b2 171 rem = do_div(avg, count); \
89ec14ce
AB
172 rem *= 1000000; \
173 do_div(rem, count); \
fb37f137 174 return sprintf(buf, "%llu.%06u\n", avg, (u32)rem); \
7a135a96 175} \
8e1d6c33 176static DEVICE_ATTR_RO(name##_avg)
7a135a96 177
8e1d6c33
BD
178#define gb_loopback_stats_attrs(field) \
179 gb_loopback_ro_stats_attr(field, min, u); \
180 gb_loopback_ro_stats_attr(field, max, u); \
181 gb_loopback_ro_avg_attr(field)
355a7058
AB
182
183#define gb_loopback_attr(field, type) \
184static ssize_t field##_show(struct device *dev, \
185 struct device_attribute *attr, \
186 char *buf) \
187{ \
079fa32b 188 struct gb_loopback *gb = dev_get_drvdata(dev); \
355a7058
AB
189 return sprintf(buf, "%"#type"\n", gb->field); \
190} \
191static ssize_t field##_store(struct device *dev, \
192 struct device_attribute *attr, \
193 const char *buf, \
194 size_t len) \
195{ \
196 int ret; \
079fa32b 197 struct gb_loopback *gb = dev_get_drvdata(dev); \
8e1d6c33 198 mutex_lock(&gb->mutex); \
355a7058 199 ret = sscanf(buf, "%"#type, &gb->field); \
355a7058 200 if (ret != 1) \
85d678c0
BD
201 len = -EINVAL; \
202 else \
8e1d6c33
BD
203 gb_loopback_check_attr(gb, bundle); \
204 mutex_unlock(&gb->mutex); \
355a7058
AB
205 return len; \
206} \
207static DEVICE_ATTR_RW(field)
208
f06272b2
BD
209#define gb_dev_loopback_ro_attr(field, conn) \
210static ssize_t field##_show(struct device *dev, \
67d1eece
BD
211 struct device_attribute *attr, \
212 char *buf) \
213{ \
079fa32b 214 struct gb_loopback *gb = dev_get_drvdata(dev); \
8e1d6c33 215 return sprintf(buf, "%u\n", gb->field); \
67d1eece
BD
216} \
217static DEVICE_ATTR_RO(field)
218
219#define gb_dev_loopback_rw_attr(field, type) \
220static ssize_t field##_show(struct device *dev, \
221 struct device_attribute *attr, \
222 char *buf) \
223{ \
079fa32b 224 struct gb_loopback *gb = dev_get_drvdata(dev); \
8e1d6c33 225 return sprintf(buf, "%"#type"\n", gb->field); \
67d1eece
BD
226} \
227static ssize_t field##_store(struct device *dev, \
228 struct device_attribute *attr, \
229 const char *buf, \
230 size_t len) \
231{ \
232 int ret; \
079fa32b 233 struct gb_loopback *gb = dev_get_drvdata(dev); \
8e1d6c33
BD
234 mutex_lock(&gb->mutex); \
235 ret = sscanf(buf, "%"#type, &gb->field); \
67d1eece
BD
236 if (ret != 1) \
237 len = -EINVAL; \
238 else \
079fa32b 239 gb_loopback_check_attr(gb); \
8e1d6c33 240 mutex_unlock(&gb->mutex); \
67d1eece
BD
241 return len; \
242} \
243static DEVICE_ATTR_RW(field)
244
8e1d6c33 245static void gb_loopback_reset_stats(struct gb_loopback *gb);
079fa32b 246static void gb_loopback_check_attr(struct gb_loopback *gb)
355a7058 247{
b36f04fa
BD
248 if (gb->us_wait > GB_LOOPBACK_US_WAIT_MAX)
249 gb->us_wait = GB_LOOPBACK_US_WAIT_MAX;
8e1d6c33
BD
250 if (gb->size > gb_dev.size_max)
251 gb->size = gb_dev.size_max;
12927835
BD
252 gb->requests_timedout = 0;
253 gb->requests_completed = 0;
8e1d6c33 254 gb->iteration_count = 0;
01480ba3 255 gb->send_count = 0;
8e1d6c33
BD
256 gb->error = 0;
257
258 if (kfifo_depth < gb->iteration_max) {
079fa32b 259 dev_warn(gb->dev,
8e1d6c33
BD
260 "cannot log bytes %u kfifo_depth %u\n",
261 gb->iteration_max, kfifo_depth);
cb60f496 262 }
8e1d6c33
BD
263 kfifo_reset_out(&gb->kfifo_lat);
264 kfifo_reset_out(&gb->kfifo_ts);
cb60f496 265
8e1d6c33 266 switch (gb->type) {
a598f438
BD
267 case GB_LOOPBACK_TYPE_PING:
268 case GB_LOOPBACK_TYPE_TRANSFER:
384a7a3c 269 case GB_LOOPBACK_TYPE_SINK:
12927835
BD
270 gb->jiffy_timeout = usecs_to_jiffies(gb->timeout);
271 if (!gb->jiffy_timeout)
272 gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MIN;
273 else if (gb->jiffy_timeout > GB_LOOPBACK_TIMEOUT_MAX)
274 gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MAX;
8e1d6c33
BD
275 gb_loopback_reset_stats(gb);
276 wake_up(&gb->wq);
a598f438
BD
277 break;
278 default:
8e1d6c33 279 gb->type = 0;
a598f438
BD
280 break;
281 }
355a7058
AB
282}
283
284/* Time to send and receive one message */
8e1d6c33 285gb_loopback_stats_attrs(latency);
583cbf50 286/* Number of requests sent per second on this cport */
8e1d6c33 287gb_loopback_stats_attrs(requests_per_second);
355a7058 288/* Quantity of data sent and received on this cport */
8e1d6c33 289gb_loopback_stats_attrs(throughput);
1ec5843e 290/* Latency across the UniPro link from APBridge's perspective */
8e1d6c33 291gb_loopback_stats_attrs(apbridge_unipro_latency);
1ec5843e 292/* Firmware induced overhead in the GPBridge */
e54b106d 293gb_loopback_stats_attrs(gbphy_firmware_latency);
8e1d6c33 294
e140c75e 295/* Number of errors encountered during loop */
8e1d6c33 296gb_loopback_ro_attr(error);
12927835
BD
297/* Number of requests successfully completed async */
298gb_loopback_ro_attr(requests_completed);
299/* Number of requests timed out async */
300gb_loopback_ro_attr(requests_timedout);
301/* Timeout minimum in useconds */
302gb_loopback_ro_attr(timeout_min);
303/* Timeout minimum in useconds */
304gb_loopback_ro_attr(timeout_max);
355a7058
AB
305
306/*
799a3f03 307 * Type of loopback message to send based on protocol type definitions
355a7058 308 * 0 => Don't send message
799a3f03 309 * 2 => Send ping message continuously (message without payload)
006335a0 310 * 3 => Send transfer message continuously (message with payload,
799a3f03
BD
311 * payload returned in response)
312 * 4 => Send a sink message (message with payload, no payload in response)
355a7058 313 */
67d1eece 314gb_dev_loopback_rw_attr(type, d);
355a7058 315/* Size of transfer message payload: 0-4096 bytes */
67d1eece 316gb_dev_loopback_rw_attr(size, u);
48f19ee8 317/* Time to wait between two messages: 0-1000 ms */
b36f04fa 318gb_dev_loopback_rw_attr(us_wait, d);
00af6583 319/* Maximum iterations for a given operation: 1-(2^32-1), 0 implies infinite */
67d1eece
BD
320gb_dev_loopback_rw_attr(iteration_max, u);
321/* The current index of the for (i = 0; i < iteration_max; i++) loop */
f06272b2 322gb_dev_loopback_ro_attr(iteration_count, false);
12927835
BD
323/* A flag to indicate synchronous or asynchronous operations */
324gb_dev_loopback_rw_attr(async, u);
325/* Timeout of an individual asynchronous request */
326gb_dev_loopback_rw_attr(timeout, u);
8e3fba55
BD
327/* Maximum number of in-flight operations before back-off */
328gb_dev_loopback_rw_attr(outstanding_operations_max, u);
355a7058 329
8e1d6c33
BD
330static struct attribute *loopback_attrs[] = {
331 &dev_attr_latency_min.attr,
332 &dev_attr_latency_max.attr,
333 &dev_attr_latency_avg.attr,
334 &dev_attr_requests_per_second_min.attr,
335 &dev_attr_requests_per_second_max.attr,
336 &dev_attr_requests_per_second_avg.attr,
337 &dev_attr_throughput_min.attr,
338 &dev_attr_throughput_max.attr,
339 &dev_attr_throughput_avg.attr,
340 &dev_attr_apbridge_unipro_latency_min.attr,
341 &dev_attr_apbridge_unipro_latency_max.attr,
342 &dev_attr_apbridge_unipro_latency_avg.attr,
e54b106d
SP
343 &dev_attr_gbphy_firmware_latency_min.attr,
344 &dev_attr_gbphy_firmware_latency_max.attr,
345 &dev_attr_gbphy_firmware_latency_avg.attr,
355a7058
AB
346 &dev_attr_type.attr,
347 &dev_attr_size.attr,
b36f04fa 348 &dev_attr_us_wait.attr,
00af6583
BD
349 &dev_attr_iteration_count.attr,
350 &dev_attr_iteration_max.attr,
12927835 351 &dev_attr_async.attr,
8e1d6c33 352 &dev_attr_error.attr,
12927835
BD
353 &dev_attr_requests_completed.attr,
354 &dev_attr_requests_timedout.attr,
355 &dev_attr_timeout.attr,
8e3fba55 356 &dev_attr_outstanding_operations_max.attr,
12927835
BD
357 &dev_attr_timeout_min.attr,
358 &dev_attr_timeout_max.attr,
355a7058
AB
359 NULL,
360};
8e1d6c33 361ATTRIBUTE_GROUPS(loopback);
355a7058 362
ab81bb9c 363static void gb_loopback_calculate_stats(struct gb_loopback *gb, bool error);
12927835 364
bd416103
BD
365static u32 gb_loopback_nsec_to_usec_latency(u64 elapsed_nsecs)
366{
367 u32 lat;
368
369 do_div(elapsed_nsecs, NSEC_PER_USEC);
370 lat = elapsed_nsecs;
371 return lat;
372}
373
7c985351
BD
374static u64 __gb_loopback_calc_latency(u64 t1, u64 t2)
375{
376 if (t2 > t1)
377 return t2 - t1;
378 else
379 return NSEC_PER_DAY - t2 + t1;
380}
381
2f842304 382static u64 gb_loopback_calc_latency(struct timeval *ts, struct timeval *te)
4c192665
BD
383{
384 u64 t1, t2;
385
386 t1 = timeval_to_ns(ts);
387 t2 = timeval_to_ns(te);
7c985351
BD
388
389 return __gb_loopback_calc_latency(t1, t2);
4c192665
BD
390}
391
4b0ea00c
BD
392static void gb_loopback_push_latency_ts(struct gb_loopback *gb,
393 struct timeval *ts, struct timeval *te)
394{
395 kfifo_in(&gb->kfifo_ts, (unsigned char *)ts, sizeof(*ts));
396 kfifo_in(&gb->kfifo_ts, (unsigned char *)te, sizeof(*te));
397}
398
fbb8edba
BD
399static int gb_loopback_operation_sync(struct gb_loopback *gb, int type,
400 void *request, int request_size,
401 void *response, int response_size)
384a7a3c 402{
fbb8edba 403 struct gb_operation *operation;
384a7a3c 404 struct timeval ts, te;
fbb8edba 405 int ret;
384a7a3c
BD
406
407 do_gettimeofday(&ts);
fbb8edba
BD
408 operation = gb_operation_create(gb->connection, type, request_size,
409 response_size, GFP_KERNEL);
d9048d8c
AB
410 if (!operation)
411 return -ENOMEM;
c3bba87a 412
fbb8edba
BD
413 if (request_size)
414 memcpy(operation->request->payload, request, request_size);
415
416 ret = gb_operation_request_send_sync(operation);
417 if (ret) {
d9a9ea1b 418 dev_err(&gb->connection->bundle->dev,
fbb8edba 419 "synchronous operation failed: %d\n", ret);
d9048d8c 420 goto out_put_operation;
fbb8edba
BD
421 } else {
422 if (response_size == operation->response->payload_size) {
423 memcpy(response, operation->response->payload,
424 response_size);
425 } else {
d9a9ea1b 426 dev_err(&gb->connection->bundle->dev,
fbb8edba
BD
427 "response size %zu expected %d\n",
428 operation->response->payload_size,
429 response_size);
d9048d8c
AB
430 ret = -EINVAL;
431 goto out_put_operation;
fbb8edba
BD
432 }
433 }
6ab1ce4d 434
384a7a3c 435 do_gettimeofday(&te);
2f842304
BD
436
437 /* Calculate the total time the message took */
4b0ea00c 438 gb_loopback_push_latency_ts(gb, &ts, &te);
2f842304
BD
439 gb->elapsed_nsecs = gb_loopback_calc_latency(&ts, &te);
440
d9048d8c
AB
441out_put_operation:
442 gb_operation_put(operation);
443
fbb8edba
BD
444 return ret;
445}
384a7a3c 446
12927835
BD
447static void __gb_loopback_async_operation_destroy(struct kref *kref)
448{
449 struct gb_loopback_async_operation *op_async;
450
451 op_async = container_of(kref, struct gb_loopback_async_operation, kref);
452
453 list_del(&op_async->entry);
454 if (op_async->operation)
455 gb_operation_put(op_async->operation);
36f241ff
BD
456 atomic_dec(&op_async->gb->outstanding_operations);
457 wake_up(&op_async->gb->wq_completion);
12927835
BD
458 kfree(op_async);
459}
460
461static void gb_loopback_async_operation_get(struct gb_loopback_async_operation
462 *op_async)
463{
464 kref_get(&op_async->kref);
465}
466
467static void gb_loopback_async_operation_put(struct gb_loopback_async_operation
468 *op_async)
469{
470 unsigned long flags;
471
472 spin_lock_irqsave(&gb_dev.lock, flags);
473 kref_put(&op_async->kref, __gb_loopback_async_operation_destroy);
474 spin_unlock_irqrestore(&gb_dev.lock, flags);
475}
476
477static struct gb_loopback_async_operation *
478 gb_loopback_operation_find(u16 id)
479{
480 struct gb_loopback_async_operation *op_async;
481 bool found = false;
482 unsigned long flags;
483
484 spin_lock_irqsave(&gb_dev.lock, flags);
485 list_for_each_entry(op_async, &gb_dev.list_op_async, entry) {
486 if (op_async->operation->id == id) {
487 gb_loopback_async_operation_get(op_async);
488 found = true;
489 break;
490 }
491 }
492 spin_unlock_irqrestore(&gb_dev.lock, flags);
493
494 return found ? op_async : NULL;
495}
496
36f241ff
BD
497static void gb_loopback_async_wait_all(struct gb_loopback *gb)
498{
499 wait_event(gb->wq_completion,
500 !atomic_read(&gb->outstanding_operations));
501}
502
12927835
BD
503static void gb_loopback_async_operation_callback(struct gb_operation *operation)
504{
505 struct gb_loopback_async_operation *op_async;
506 struct gb_loopback *gb;
507 struct timeval te;
508 bool err = false;
509
510 do_gettimeofday(&te);
511 op_async = gb_loopback_operation_find(operation->id);
512 if (!op_async)
513 return;
514
515 gb = op_async->gb;
516 mutex_lock(&gb->mutex);
517
518 if (!op_async->pending || gb_operation_result(operation)) {
519 err = true;
520 } else {
521 if (op_async->completion)
522 if (op_async->completion(op_async))
523 err = true;
524 }
525
41993cd5 526 if (!err) {
12927835
BD
527 gb_loopback_push_latency_ts(gb, &op_async->ts, &te);
528 gb->elapsed_nsecs = gb_loopback_calc_latency(&op_async->ts,
529 &te);
12927835
BD
530 }
531
532 if (op_async->pending) {
41993cd5
JH
533 if (err)
534 gb->error++;
12927835
BD
535 gb->iteration_count++;
536 op_async->pending = false;
537 del_timer_sync(&op_async->timer);
538 gb_loopback_async_operation_put(op_async);
ab81bb9c 539 gb_loopback_calculate_stats(gb, err);
12927835
BD
540 }
541 mutex_unlock(&gb->mutex);
542
543 dev_dbg(&gb->connection->bundle->dev, "complete operation %d\n",
544 operation->id);
545
546 gb_loopback_async_operation_put(op_async);
547}
548
549static void gb_loopback_async_operation_work(struct work_struct *work)
550{
551 struct gb_loopback *gb;
552 struct gb_operation *operation;
553 struct gb_loopback_async_operation *op_async;
554
555 op_async = container_of(work, struct gb_loopback_async_operation, work);
12927835
BD
556 gb = op_async->gb;
557 operation = op_async->operation;
558
559 mutex_lock(&gb->mutex);
560 if (op_async->pending) {
561 gb->requests_timedout++;
562 gb->error++;
563 gb->iteration_count++;
564 op_async->pending = false;
565 gb_loopback_async_operation_put(op_async);
ab81bb9c 566 gb_loopback_calculate_stats(gb, true);
12927835
BD
567 }
568 mutex_unlock(&gb->mutex);
569
570 dev_dbg(&gb->connection->bundle->dev, "timeout operation %d\n",
571 operation->id);
572
573 gb_operation_cancel(operation, -ETIMEDOUT);
574 gb_loopback_async_operation_put(op_async);
575}
576
577static void gb_loopback_async_operation_timeout(unsigned long data)
578{
579 struct gb_loopback_async_operation *op_async;
580 u16 id = data;
581
582 op_async = gb_loopback_operation_find(id);
583 if (!op_async) {
584 pr_err("operation %d not found - time out ?\n", id);
585 return;
586 }
587 schedule_work(&op_async->work);
588}
589
590static int gb_loopback_async_operation(struct gb_loopback *gb, int type,
591 void *request, int request_size,
592 int response_size,
593 void *completion)
594{
595 struct gb_loopback_async_operation *op_async;
596 struct gb_operation *operation;
597 int ret;
598 unsigned long flags;
599
600 op_async = kzalloc(sizeof(*op_async), GFP_KERNEL);
601 if (!op_async)
602 return -ENOMEM;
603
604 INIT_WORK(&op_async->work, gb_loopback_async_operation_work);
605 init_timer(&op_async->timer);
606 kref_init(&op_async->kref);
607
608 operation = gb_operation_create(gb->connection, type, request_size,
609 response_size, GFP_KERNEL);
610 if (!operation) {
c7aae4e6
BD
611 kfree(op_async);
612 return -ENOMEM;
12927835
BD
613 }
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;
36f241ff 628 atomic_inc(&gb->outstanding_operations);
fece9c87 629 mutex_lock(&gb->mutex);
12927835
BD
630 ret = gb_operation_request_send(operation,
631 gb_loopback_async_operation_callback,
632 GFP_KERNEL);
633 if (ret)
634 goto error;
635
636 op_async->timer.function = gb_loopback_async_operation_timeout;
637 op_async->timer.expires = jiffies + gb->jiffy_timeout;
638 op_async->timer.data = (unsigned long)operation->id;
639 add_timer(&op_async->timer);
640
fece9c87 641 goto done;
12927835
BD
642error:
643 gb_loopback_async_operation_put(op_async);
fece9c87
BD
644done:
645 mutex_unlock(&gb->mutex);
12927835
BD
646 return ret;
647}
648
649static int gb_loopback_sync_sink(struct gb_loopback *gb, u32 len)
fbb8edba
BD
650{
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);
384a7a3c
BD
662 kfree(request);
663 return retval;
664}
665
12927835 666static int gb_loopback_sync_transfer(struct gb_loopback *gb, u32 len)
355a7058 667{
355a7058
AB
668 struct gb_loopback_transfer_request *request;
669 struct gb_loopback_transfer_response *response;
670 int retval;
671
d6a1a3b5 672 gb->apbridge_latency_ts = 0;
e54b106d 673 gb->gbphy_latency_ts = 0;
d6a1a3b5 674
355a7058
AB
675 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
dc4a1069
VK
684 memset(request->data, 0x5A, len);
685
355a7058 686 request->len = cpu_to_le32(len);
fbb8edba
BD
687 retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_TRANSFER,
688 request, len + sizeof(*request),
689 response, len + sizeof(*response));
355a7058
AB
690 if (retval)
691 goto gb_error;
692
dc366f8e 693 if (memcmp(request->data, response->data, len)) {
d9a9ea1b
GKH
694 dev_err(&gb->connection->bundle->dev,
695 "Loopback Data doesn't match\n");
355a7058 696 retval = -EREMOTEIO;
dc366f8e 697 }
1ec5843e 698 gb->apbridge_latency_ts = (u32)__le32_to_cpu(response->reserved0);
e54b106d 699 gb->gbphy_latency_ts = (u32)__le32_to_cpu(response->reserved1);
355a7058
AB
700
701gb_error:
702 kfree(request);
703 kfree(response);
704
705 return retval;
706}
707
12927835 708static int gb_loopback_sync_ping(struct gb_loopback *gb)
355a7058 709{
fbb8edba
BD
710 return gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_PING,
711 NULL, 0, NULL, 0);
355a7058
AB
712}
713
12927835
BD
714static 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);
e54b106d 755 gb->gbphy_latency_ts =
12927835
BD
756 (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
e82a11dc 793static int gb_loopback_request_handler(struct gb_operation *operation)
ac1c2840
AE
794{
795 struct gb_connection *connection = operation->connection;
796 struct gb_loopback_transfer_request *request;
797 struct gb_loopback_transfer_response *response;
d9a9ea1b 798 struct device *dev = &connection->bundle->dev;
e51eafeb 799 size_t len;
ac1c2840
AE
800
801 /* By convention, the AP initiates the version operation */
e82a11dc 802 switch (operation->type) {
ac1c2840 803 case GB_LOOPBACK_TYPE_PING:
384a7a3c 804 case GB_LOOPBACK_TYPE_SINK:
ac1c2840
AE
805 return 0;
806 case GB_LOOPBACK_TYPE_TRANSFER:
807 if (operation->request->payload_size < sizeof(*request)) {
d9a9ea1b 808 dev_err(dev, "transfer request too small (%zu < %zu)\n",
ac1c2840
AE
809 operation->request->payload_size,
810 sizeof(*request));
811 return -EINVAL; /* -EMSGSIZE */
812 }
813 request = operation->request->payload;
814 len = le32_to_cpu(request->len);
67d1eece 815 if (len > gb_dev.size_max) {
d9a9ea1b 816 dev_err(dev, "transfer request too large (%zu > %zu)\n",
67d1eece 817 len, gb_dev.size_max);
c3bba87a
BD
818 return -EINVAL;
819 }
820
81ad6994
BG
821 if (!gb_operation_response_alloc(operation,
822 len + sizeof(*response), GFP_KERNEL)) {
823 dev_err(dev, "error allocating response\n");
824 return -ENOMEM;
ac1c2840 825 }
81ad6994
BG
826 response = operation->response->payload;
827 response->len = cpu_to_le32(len);
828 if (len)
829 memcpy(response->data, request->data, len);
830
ac1c2840
AE
831 return 0;
832 default:
e82a11dc 833 dev_err(dev, "unsupported request: %u\n", operation->type);
ac1c2840
AE
834 return -EINVAL;
835 }
836}
837
8e1d6c33 838static void gb_loopback_reset_stats(struct gb_loopback *gb)
355a7058
AB
839{
840 struct gb_loopback_stats reset = {
e051c82b 841 .min = U32_MAX,
355a7058 842 };
67d1eece
BD
843
844 /* Reset per-connection stats */
8e1d6c33
BD
845 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));
e54b106d 853 memcpy(&gb->gbphy_firmware_latency, &reset,
8e1d6c33 854 sizeof(struct gb_loopback_stats));
67d1eece 855
f42a6891
BD
856 /* Should be initialized at least once per transaction set */
857 gb->apbridge_latency_ts = 0;
e54b106d 858 gb->gbphy_latency_ts = 0;
ab81bb9c 859 memset(&gb->ts, 0, sizeof(struct timeval));
355a7058
AB
860}
861
a6e7e535 862static void gb_loopback_update_stats(struct gb_loopback_stats *stats, u32 val)
355a7058 863{
00af6583
BD
864 if (stats->min > val)
865 stats->min = val;
866 if (stats->max < val)
867 stats->max = val;
868 stats->sum += val;
869 stats->count++;
355a7058
AB
870}
871
ab81bb9c
AB
872static 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
583cbf50 885static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency)
355a7058 886{
ab81bb9c 887 u64 req = gb->requests_completed * USEC_PER_SEC;
00af6583 888
ab81bb9c 889 gb_loopback_update_stats_window(&gb->requests_per_second, req, latency);
355a7058
AB
890}
891
00af6583 892static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency)
355a7058 893{
ab81bb9c 894 u64 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2;
f7908e4d 895
8e1d6c33 896 switch (gb->type) {
f7908e4d
BD
897 case GB_LOOPBACK_TYPE_PING:
898 break;
899 case GB_LOOPBACK_TYPE_SINK:
900 aggregate_size += sizeof(struct gb_loopback_transfer_request) +
8e1d6c33 901 gb->size;
f7908e4d
BD
902 break;
903 case GB_LOOPBACK_TYPE_TRANSFER:
904 aggregate_size += sizeof(struct gb_loopback_transfer_request) +
905 sizeof(struct gb_loopback_transfer_response) +
8e1d6c33 906 gb->size * 2;
f7908e4d
BD
907 break;
908 default:
909 return;
910 }
00af6583 911
ab81bb9c
AB
912 aggregate_size *= gb->requests_completed;
913 aggregate_size *= USEC_PER_SEC;
914 gb_loopback_update_stats_window(&gb->throughput, aggregate_size,
915 latency);
355a7058
AB
916}
917
ab81bb9c 918static void gb_loopback_calculate_latency_stats(struct gb_loopback *gb)
355a7058
AB
919{
920 u32 lat;
355a7058 921
00af6583 922 /* Express latency in terms of microseconds */
bd416103 923 lat = gb_loopback_nsec_to_usec_latency(gb->elapsed_nsecs);
355a7058 924
98676ca8 925 /* Log latency stastic */
00af6583 926 gb_loopback_update_stats(&gb->latency, lat);
67d1eece
BD
927
928 /* Raw latency log on a per thread basis */
4b0ea00c 929 kfifo_in(&gb->kfifo_lat, (unsigned char *)&lat, sizeof(lat));
00af6583 930
1ec5843e 931 /* Log the firmware supplied latency values */
1ec5843e
BD
932 gb_loopback_update_stats(&gb->apbridge_unipro_latency,
933 gb->apbridge_latency_ts);
e54b106d
SP
934 gb_loopback_update_stats(&gb->gbphy_firmware_latency,
935 gb->gbphy_latency_ts);
355a7058
AB
936}
937
ab81bb9c
AB
938static 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
8e3fba55
BD
964static 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
355a7058
AB
974static int gb_loopback_fn(void *data)
975{
976 int error = 0;
b36f04fa 977 int us_wait = 0;
67d1eece
BD
978 int type;
979 u32 size;
01480ba3 980
3f12c3ed 981 struct gb_loopback *gb = data;
355a7058 982
3dfe8aaa 983 while (1) {
8e1d6c33
BD
984 if (!gb->type)
985 wait_event_interruptible(gb->wq, gb->type ||
3dfe8aaa 986 kthread_should_stop());
8e3fba55
BD
987 if (kthread_should_stop())
988 break;
12927835 989
8e3fba55
BD
990 /* Limit the maximum number of in-flight async operations */
991 gb_loopback_async_wait_to_send(gb);
3dfe8aaa
BD
992 if (kthread_should_stop())
993 break;
85d678c0 994
67d1eece 995 mutex_lock(&gb->mutex);
8e1d6c33
BD
996
997 /* Optionally terminate */
01480ba3 998 if (gb->send_count == gb->iteration_max) {
ab81bb9c
AB
999 if (gb->iteration_count == gb->iteration_max) {
1000 gb->type = 0;
01480ba3 1001 gb->send_count = 0;
39c2787b
AH
1002 sysfs_notify(&gb->dev->kobj, NULL,
1003 "iteration_count");
ab81bb9c 1004 }
67d1eece 1005 mutex_unlock(&gb->mutex);
8e1d6c33 1006 continue;
67d1eece 1007 }
8e1d6c33 1008 size = gb->size;
b36f04fa 1009 us_wait = gb->us_wait;
8e1d6c33 1010 type = gb->type;
ab81bb9c
AB
1011 if (gb->ts.tv_usec == 0 && gb->ts.tv_sec == 0)
1012 do_gettimeofday(&gb->ts);
d9fb3754
BD
1013 mutex_unlock(&gb->mutex);
1014
67d1eece 1015 /* Else operations to perform */
12927835
BD
1016 if (gb->async) {
1017 if (type == GB_LOOPBACK_TYPE_PING) {
1018 error = gb_loopback_async_ping(gb);
12927835
BD
1019 } else if (type == GB_LOOPBACK_TYPE_TRANSFER) {
1020 error = gb_loopback_async_transfer(gb, size);
1021 } else if (type == GB_LOOPBACK_TYPE_SINK) {
1022 error = gb_loopback_async_sink(gb, size);
1023 }
1024
1025 if (error)
1026 gb->error++;
1027 } else {
1028 /* We are effectively single threaded here */
1029 if (type == GB_LOOPBACK_TYPE_PING)
1030 error = gb_loopback_sync_ping(gb);
1031 else if (type == GB_LOOPBACK_TYPE_TRANSFER)
1032 error = gb_loopback_sync_transfer(gb, size);
1033 else if (type == GB_LOOPBACK_TYPE_SINK)
1034 error = gb_loopback_sync_sink(gb, size);
1035
1036 if (error)
1037 gb->error++;
1038 gb->iteration_count++;
ab81bb9c 1039 gb_loopback_calculate_stats(gb, !!error);
12927835 1040 }
01480ba3 1041 gb->send_count++;
b36f04fa
BD
1042 if (us_wait)
1043 udelay(us_wait);
355a7058
AB
1044 }
1045 return 0;
1046}
1047
4b0ea00c
BD
1048static int gb_loopback_dbgfs_latency_show_common(struct seq_file *s,
1049 struct kfifo *kfifo,
1050 struct mutex *mutex)
aa27bf82 1051{
aa27bf82
BD
1052 u32 latency;
1053 int retval;
1054
4b0ea00c 1055 if (kfifo_len(kfifo) == 0) {
aa27bf82
BD
1056 retval = -EAGAIN;
1057 goto done;
1058 }
1059
4b0ea00c
BD
1060 mutex_lock(mutex);
1061 retval = kfifo_out(kfifo, &latency, sizeof(latency));
aa27bf82
BD
1062 if (retval > 0) {
1063 seq_printf(s, "%u", latency);
1064 retval = 0;
1065 }
4b0ea00c 1066 mutex_unlock(mutex);
aa27bf82
BD
1067done:
1068 return retval;
1069}
1070
4b0ea00c
BD
1071static int gb_loopback_dbgfs_latency_show(struct seq_file *s, void *unused)
1072{
1073 struct gb_loopback *gb = s->private;
1074
1075 return gb_loopback_dbgfs_latency_show_common(s, &gb->kfifo_lat,
1076 &gb->mutex);
1077}
1078
aa27bf82
BD
1079static int gb_loopback_latency_open(struct inode *inode, struct file *file)
1080{
1081 return single_open(file, gb_loopback_dbgfs_latency_show,
1082 inode->i_private);
1083}
1084
1085static const struct file_operations gb_loopback_debugfs_latency_ops = {
1086 .open = gb_loopback_latency_open,
1087 .read = seq_read,
1088 .llseek = seq_lseek,
1089 .release = single_release,
1090};
1091
5015115d
BD
1092static int gb_loopback_bus_id_compare(void *priv, struct list_head *lha,
1093 struct list_head *lhb)
1094{
1095 struct gb_loopback *a = list_entry(lha, struct gb_loopback, entry);
1096 struct gb_loopback *b = list_entry(lhb, struct gb_loopback, entry);
1097 struct gb_connection *ca = a->connection;
1098 struct gb_connection *cb = b->connection;
1099
5015115d
BD
1100 if (ca->bundle->intf->interface_id < cb->bundle->intf->interface_id)
1101 return -1;
1102 if (cb->bundle->intf->interface_id < ca->bundle->intf->interface_id)
1103 return 1;
1104 if (ca->bundle->id < cb->bundle->id)
1105 return -1;
1106 if (cb->bundle->id < ca->bundle->id)
1107 return 1;
1108 if (ca->intf_cport_id < cb->intf_cport_id)
1109 return -1;
1110 else if (cb->intf_cport_id < ca->intf_cport_id)
1111 return 1;
1112
1113 return 0;
1114}
1115
1116static void gb_loopback_insert_id(struct gb_loopback *gb)
1117{
1118 struct gb_loopback *gb_list;
1119 u32 new_lbid = 0;
1120
1121 /* perform an insertion sort */
1122 list_add_tail(&gb->entry, &gb_dev.list);
1123 list_sort(NULL, &gb_dev.list, gb_loopback_bus_id_compare);
1124 list_for_each_entry(gb_list, &gb_dev.list, entry) {
1125 gb_list->lbid = 1 << new_lbid;
1126 new_lbid++;
1127 }
1128}
1129
67d1eece 1130#define DEBUGFS_NAMELEN 32
aa27bf82 1131
e82a11dc
VK
1132static int gb_loopback_probe(struct gb_bundle *bundle,
1133 const struct greybus_bundle_id *id)
355a7058 1134{
e82a11dc
VK
1135 struct greybus_descriptor_cport *cport_desc;
1136 struct gb_connection *connection;
355a7058 1137 struct gb_loopback *gb;
079fa32b 1138 struct device *dev;
355a7058 1139 int retval;
aa27bf82 1140 char name[DEBUGFS_NAMELEN];
2e238d71 1141 unsigned long flags;
355a7058 1142
e82a11dc
VK
1143 if (bundle->num_cports != 1)
1144 return -ENODEV;
1145
1146 cport_desc = &bundle->cport_desc[0];
1147 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LOOPBACK)
1148 return -ENODEV;
1149
355a7058
AB
1150 gb = kzalloc(sizeof(*gb), GFP_KERNEL);
1151 if (!gb)
1152 return -ENOMEM;
1153
e82a11dc
VK
1154 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1155 gb_loopback_request_handler);
1156 if (IS_ERR(connection)) {
1157 retval = PTR_ERR(connection);
1158 goto out_kzalloc;
1159 }
1160
1161 gb->connection = connection;
1162 greybus_set_drvdata(bundle, gb);
1163
8e1d6c33 1164 init_waitqueue_head(&gb->wq);
36f241ff
BD
1165 init_waitqueue_head(&gb->wq_completion);
1166 atomic_set(&gb->outstanding_operations, 0);
8e1d6c33
BD
1167 gb_loopback_reset_stats(gb);
1168
12927835
BD
1169 /* Reported values to user-space for min/max timeouts */
1170 gb->timeout_min = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MIN);
1171 gb->timeout_max = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MAX);
1172
f06272b2 1173 if (!gb_dev.count) {
f06272b2
BD
1174 /* Calculate maximum payload */
1175 gb_dev.size_max = gb_operation_get_payload_size_max(connection);
1176 if (gb_dev.size_max <=
1177 sizeof(struct gb_loopback_transfer_request)) {
1178 retval = -EINVAL;
e82a11dc 1179 goto out_connection_destroy;
f06272b2
BD
1180 }
1181 gb_dev.size_max -= sizeof(struct gb_loopback_transfer_request);
1182 }
f06272b2
BD
1183
1184 /* Create per-connection sysfs and debugfs data-points */
8d8d36da 1185 snprintf(name, sizeof(name), "raw_latency_%s",
d9a9ea1b 1186 dev_name(&connection->bundle->dev));
aa27bf82
BD
1187 gb->file = debugfs_create_file(name, S_IFREG | S_IRUGO, gb_dev.root, gb,
1188 &gb_loopback_debugfs_latency_ops);
079fa32b
AH
1189
1190 gb->id = ida_simple_get(&loopback_ida, 0, 0, GFP_KERNEL);
1191 if (gb->id < 0) {
1192 retval = gb->id;
e82a11dc 1193 goto out_debugfs_remove;
079fa32b
AH
1194 }
1195
e82a11dc
VK
1196 retval = gb_connection_enable(connection);
1197 if (retval)
1198 goto out_ida_remove;
1199
079fa32b
AH
1200 dev = device_create_with_groups(&loopback_class,
1201 &connection->bundle->dev,
1202 MKDEV(0, 0), gb, loopback_groups,
1203 "gb_loopback%d", gb->id);
1204 if (IS_ERR(dev)) {
1205 retval = PTR_ERR(dev);
e82a11dc 1206 goto out_connection_disable;
079fa32b
AH
1207 }
1208 gb->dev = dev;
c3bba87a 1209
cbd204b4 1210 /* Allocate kfifo */
4b0ea00c 1211 if (kfifo_alloc(&gb->kfifo_lat, kfifo_depth * sizeof(u32),
cbd204b4
BD
1212 GFP_KERNEL)) {
1213 retval = -ENOMEM;
079fa32b 1214 goto out_conn;
cbd204b4 1215 }
4b0ea00c
BD
1216 if (kfifo_alloc(&gb->kfifo_ts, kfifo_depth * sizeof(struct timeval) * 2,
1217 GFP_KERNEL)) {
1218 retval = -ENOMEM;
1219 goto out_kfifo0;
1220 }
cbd204b4
BD
1221
1222 /* Fork worker thread */
85d678c0 1223 mutex_init(&gb->mutex);
355a7058
AB
1224 gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback");
1225 if (IS_ERR(gb->task)) {
69f60347 1226 retval = PTR_ERR(gb->task);
4b0ea00c 1227 goto out_kfifo1;
355a7058
AB
1228 }
1229
2e238d71 1230 spin_lock_irqsave(&gb_dev.lock, flags);
5015115d 1231 gb_loopback_insert_id(gb);
aa27bf82 1232 gb_dev.count++;
2e238d71
BD
1233 spin_unlock_irqrestore(&gb_dev.lock, flags);
1234
1235 gb_connection_latency_tag_enable(connection);
355a7058
AB
1236 return 0;
1237
4b0ea00c
BD
1238out_kfifo1:
1239 kfifo_free(&gb->kfifo_ts);
1240out_kfifo0:
1241 kfifo_free(&gb->kfifo_lat);
079fa32b
AH
1242out_conn:
1243 device_unregister(dev);
e82a11dc
VK
1244out_connection_disable:
1245 gb_connection_disable(connection);
1246out_ida_remove:
079fa32b 1247 ida_simple_remove(&loopback_ida, gb->id);
e82a11dc 1248out_debugfs_remove:
aa27bf82 1249 debugfs_remove(gb->file);
e82a11dc
VK
1250out_connection_destroy:
1251 gb_connection_destroy(connection);
2e238d71 1252out_kzalloc:
355a7058 1253 kfree(gb);
1fb807cf 1254
355a7058
AB
1255 return retval;
1256}
1257
e82a11dc 1258static void gb_loopback_disconnect(struct gb_bundle *bundle)
355a7058 1259{
e82a11dc 1260 struct gb_loopback *gb = greybus_get_drvdata(bundle);
2e238d71 1261 unsigned long flags;
355a7058 1262
e82a11dc
VK
1263 gb_connection_disable(gb->connection);
1264
355a7058
AB
1265 if (!IS_ERR_OR_NULL(gb->task))
1266 kthread_stop(gb->task);
cbd204b4 1267
4b0ea00c
BD
1268 kfifo_free(&gb->kfifo_lat);
1269 kfifo_free(&gb->kfifo_ts);
e82a11dc 1270 gb_connection_latency_tag_disable(gb->connection);
aa27bf82 1271 debugfs_remove(gb->file);
e82a11dc
VK
1272
1273 /*
1274 * FIXME: gb_loopback_async_wait_all() is redundant now, as connection
1275 * is disabled at the beginning and so we can't have any more
1276 * incoming/outgoing requests.
1277 */
36f241ff 1278 gb_loopback_async_wait_all(gb);
2e238d71
BD
1279
1280 spin_lock_irqsave(&gb_dev.lock, flags);
1281 gb_dev.count--;
ff477d07 1282 list_del(&gb->entry);
2e238d71
BD
1283 spin_unlock_irqrestore(&gb_dev.lock, flags);
1284
079fa32b
AH
1285 device_unregister(gb->dev);
1286 ida_simple_remove(&loopback_ida, gb->id);
1287
e82a11dc 1288 gb_connection_destroy(gb->connection);
5f3e0d17 1289 kfree(gb);
355a7058
AB
1290}
1291
e82a11dc
VK
1292static const struct greybus_bundle_id gb_loopback_id_table[] = {
1293 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOOPBACK) },
1294 { }
1295};
1296MODULE_DEVICE_TABLE(greybus, gb_loopback_id_table);
1297
1298static struct greybus_driver gb_loopback_driver = {
1299 .name = "loopback",
1300 .probe = gb_loopback_probe,
1301 .disconnect = gb_loopback_disconnect,
1302 .id_table = gb_loopback_id_table,
355a7058
AB
1303};
1304
cbd204b4
BD
1305static int loopback_init(void)
1306{
4b0ea00c
BD
1307 int retval;
1308
67d1eece 1309 INIT_LIST_HEAD(&gb_dev.list);
12927835 1310 INIT_LIST_HEAD(&gb_dev.list_op_async);
2e238d71 1311 spin_lock_init(&gb_dev.lock);
aa27bf82 1312 gb_dev.root = debugfs_create_dir("gb_loopback", NULL);
67d1eece 1313
079fa32b
AH
1314 retval = class_register(&loopback_class);
1315 if (retval)
1316 goto err;
1317
e82a11dc 1318 retval = greybus_register(&gb_loopback_driver);
079fa32b
AH
1319 if (retval)
1320 goto err_unregister;
1321
1322 return 0;
4b0ea00c 1323
079fa32b
AH
1324err_unregister:
1325 class_unregister(&loopback_class);
1326err:
4b0ea00c
BD
1327 debugfs_remove_recursive(gb_dev.root);
1328 return retval;
cbd204b4
BD
1329}
1330module_init(loopback_init);
1331
1332static void __exit loopback_exit(void)
1333{
aa27bf82 1334 debugfs_remove_recursive(gb_dev.root);
e82a11dc 1335 greybus_deregister(&gb_loopback_driver);
079fa32b
AH
1336 class_unregister(&loopback_class);
1337 ida_destroy(&loopback_ida);
cbd204b4
BD
1338}
1339module_exit(loopback_exit);
355a7058
AB
1340
1341MODULE_LICENSE("GPL v2");