Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 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 | */ |
Johan Hovold | 8923c5b | 2016-02-11 13:52:49 +0100 | [diff] [blame] | 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/kthread.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/random.h> |
Greg Kroah-Hartman | 5679f78 | 2015-03-31 23:01:45 +0200 | [diff] [blame] | 19 | #include <linux/sizes.h> |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 20 | #include <linux/cdev.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/kfifo.h> |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 23 | #include <linux/debugfs.h> |
Bryan O'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 24 | #include <linux/list_sort.h> |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 27 | #include <linux/atomic.h> |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 28 | #include <linux/pm_runtime.h> |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 29 | |
Johan Hovold | 1ffc12b | 2015-04-16 09:53:59 +0200 | [diff] [blame] | 30 | #include <asm/div64.h> |
| 31 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 32 | #include "greybus.h" |
Bryan O'Donoghue | fd58926 | 2015-10-15 16:10:43 +0100 | [diff] [blame] | 33 | #include "connection.h" |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 34 | |
Bryan O'Donoghue | fd489e1 | 2015-08-11 13:50:54 +0100 | [diff] [blame] | 35 | #define NSEC_PER_DAY 86400000000000ULL |
| 36 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 37 | struct gb_loopback_stats { |
Alex Elder | e051c82 | 2015-08-03 12:57:14 -0500 | [diff] [blame] | 38 | u32 min; |
| 39 | u32 max; |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 40 | u64 sum; |
Alex Elder | e13fc28 | 2015-08-03 12:57:15 -0500 | [diff] [blame] | 41 | u32 count; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 44 | struct gb_loopback_device { |
| 45 | struct dentry *root; |
| 46 | u32 count; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 47 | size_t size_max; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 48 | |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 49 | /* We need to take a lock in atomic context */ |
| 50 | spinlock_t lock; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 51 | struct list_head list; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 52 | struct list_head list_op_async; |
| 53 | wait_queue_head_t wq; |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static struct gb_loopback_device gb_dev; |
| 57 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 58 | struct 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 Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 70 | struct gb_loopback { |
| 71 | struct gb_connection *connection; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 72 | |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 73 | struct dentry *file; |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 74 | struct kfifo kfifo_lat; |
| 75 | struct kfifo kfifo_ts; |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 76 | struct mutex mutex; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 77 | struct task_struct *task; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 78 | struct list_head entry; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 79 | struct device *dev; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 80 | wait_queue_head_t wq; |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 81 | wait_queue_head_t wq_completion; |
| 82 | atomic_t outstanding_operations; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 83 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 84 | /* Per connection stats */ |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 85 | struct timeval ts; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 86 | struct gb_loopback_stats latency; |
| 87 | struct gb_loopback_stats throughput; |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 88 | struct gb_loopback_stats requests_per_second; |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 89 | struct gb_loopback_stats apbridge_unipro_latency; |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 90 | struct gb_loopback_stats gbphy_firmware_latency; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 91 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 92 | int type; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 93 | int async; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 94 | int id; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 95 | u32 size; |
| 96 | u32 iteration_max; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 97 | u32 iteration_count; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 98 | int us_wait; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 99 | u32 error; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 100 | u32 requests_completed; |
| 101 | u32 requests_timedout; |
| 102 | u32 timeout; |
| 103 | u32 jiffy_timeout; |
| 104 | u32 timeout_min; |
| 105 | u32 timeout_max; |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 106 | u32 outstanding_operations_max; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 107 | u32 lbid; |
| 108 | u64 elapsed_nsecs; |
Bryan O'Donoghue | e6227ee | 2015-10-15 16:10:44 +0100 | [diff] [blame] | 109 | u32 apbridge_latency_ts; |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 110 | u32 gbphy_latency_ts; |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 111 | |
| 112 | u32 send_count; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 115 | static struct class loopback_class = { |
| 116 | .name = "gb_loopback", |
| 117 | .owner = THIS_MODULE, |
| 118 | }; |
| 119 | static DEFINE_IDA(loopback_ida); |
| 120 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 121 | /* Min/max values in jiffies */ |
| 122 | #define GB_LOOPBACK_TIMEOUT_MIN 1 |
| 123 | #define GB_LOOPBACK_TIMEOUT_MAX 10000 |
| 124 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 125 | #define GB_LOOPBACK_FIFO_DEFAULT 8192 |
| 126 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 127 | static unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT; |
| 128 | module_param(kfifo_depth, uint, 0444); |
| 129 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 130 | /* Maximum size of any one send data buffer we support */ |
| 131 | #define MAX_PACKET_SIZE (PAGE_SIZE * 2) |
| 132 | |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 133 | #define GB_LOOPBACK_US_WAIT_MAX 1000000 |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 134 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 135 | /* interface sysfs attributes */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 136 | #define gb_loopback_ro_attr(field) \ |
| 137 | static ssize_t field##_show(struct device *dev, \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 138 | struct device_attribute *attr, \ |
| 139 | char *buf) \ |
| 140 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 141 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 142 | return sprintf(buf, "%u\n", gb->field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 143 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 144 | static DEVICE_ATTR_RO(field) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 145 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 146 | #define gb_loopback_ro_stats_attr(name, field, type) \ |
| 147 | static ssize_t name##_##field##_show(struct device *dev, \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 148 | struct device_attribute *attr, \ |
| 149 | char *buf) \ |
| 150 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 151 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Alexandre Bailon | 1dc8d3d | 2016-03-11 17:23:37 +0100 | [diff] [blame] | 152 | /* Report 0 for min and max if no transfer successed */ \ |
| 153 | if (!gb->requests_completed) \ |
| 154 | return sprintf(buf, "0\n"); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 155 | return sprintf(buf, "%"#type"\n", gb->name.field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 156 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 157 | static DEVICE_ATTR_RO(name##_##field) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 158 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 159 | #define gb_loopback_ro_avg_attr(name) \ |
| 160 | static ssize_t name##_avg_show(struct device *dev, \ |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 161 | struct device_attribute *attr, \ |
| 162 | char *buf) \ |
| 163 | { \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 164 | struct gb_loopback_stats *stats; \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 165 | struct gb_loopback *gb; \ |
Alexandre Bailon | fb37f13 | 2016-02-25 18:19:16 +0100 | [diff] [blame] | 166 | u64 avg, rem; \ |
| 167 | u32 count; \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 168 | gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 169 | stats = &gb->name; \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 170 | count = stats->count ? stats->count : 1; \ |
Alexandre Bailon | 58a527a | 2016-03-08 16:37:37 +0100 | [diff] [blame] | 171 | avg = stats->sum + count / 2000000; /* round closest */ \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 172 | rem = do_div(avg, count); \ |
Alexandre Bailon | 89ec14c | 2016-03-08 16:37:36 +0100 | [diff] [blame] | 173 | rem *= 1000000; \ |
| 174 | do_div(rem, count); \ |
Alexandre Bailon | fb37f13 | 2016-02-25 18:19:16 +0100 | [diff] [blame] | 175 | return sprintf(buf, "%llu.%06u\n", avg, (u32)rem); \ |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 176 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 177 | static DEVICE_ATTR_RO(name##_avg) |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 178 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 179 | #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 Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 183 | |
| 184 | #define gb_loopback_attr(field, type) \ |
| 185 | static ssize_t field##_show(struct device *dev, \ |
| 186 | struct device_attribute *attr, \ |
| 187 | char *buf) \ |
| 188 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 189 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 190 | return sprintf(buf, "%"#type"\n", gb->field); \ |
| 191 | } \ |
| 192 | static 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 Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 198 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 199 | mutex_lock(&gb->mutex); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 200 | ret = sscanf(buf, "%"#type, &gb->field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 201 | if (ret != 1) \ |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 202 | len = -EINVAL; \ |
| 203 | else \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 204 | gb_loopback_check_attr(gb, bundle); \ |
| 205 | mutex_unlock(&gb->mutex); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 206 | return len; \ |
| 207 | } \ |
| 208 | static DEVICE_ATTR_RW(field) |
| 209 | |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 210 | #define gb_dev_loopback_ro_attr(field, conn) \ |
| 211 | static ssize_t field##_show(struct device *dev, \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 212 | struct device_attribute *attr, \ |
| 213 | char *buf) \ |
| 214 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 215 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 216 | return sprintf(buf, "%u\n", gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 217 | } \ |
| 218 | static DEVICE_ATTR_RO(field) |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 219 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 220 | #define gb_dev_loopback_rw_attr(field, type) \ |
| 221 | static ssize_t field##_show(struct device *dev, \ |
| 222 | struct device_attribute *attr, \ |
| 223 | char *buf) \ |
| 224 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 225 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 226 | return sprintf(buf, "%"#type"\n", gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 227 | } \ |
| 228 | static 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 Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 234 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 235 | mutex_lock(&gb->mutex); \ |
| 236 | ret = sscanf(buf, "%"#type, &gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 237 | if (ret != 1) \ |
| 238 | len = -EINVAL; \ |
| 239 | else \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 240 | gb_loopback_check_attr(gb); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 241 | mutex_unlock(&gb->mutex); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 242 | return len; \ |
| 243 | } \ |
| 244 | static DEVICE_ATTR_RW(field) |
| 245 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 246 | static void gb_loopback_reset_stats(struct gb_loopback *gb); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 247 | static void gb_loopback_check_attr(struct gb_loopback *gb) |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 248 | { |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 249 | if (gb->us_wait > GB_LOOPBACK_US_WAIT_MAX) |
| 250 | gb->us_wait = GB_LOOPBACK_US_WAIT_MAX; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 251 | if (gb->size > gb_dev.size_max) |
| 252 | gb->size = gb_dev.size_max; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 253 | gb->requests_timedout = 0; |
| 254 | gb->requests_completed = 0; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 255 | gb->iteration_count = 0; |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 256 | gb->send_count = 0; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 257 | gb->error = 0; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 258 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 259 | if (kfifo_depth < gb->iteration_max) { |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 260 | dev_warn(gb->dev, |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 261 | "cannot log bytes %u kfifo_depth %u\n", |
| 262 | gb->iteration_max, kfifo_depth); |
Bryan O'Donoghue | cb60f49 | 2015-07-28 18:34:39 +0100 | [diff] [blame] | 263 | } |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 264 | kfifo_reset_out(&gb->kfifo_lat); |
| 265 | kfifo_reset_out(&gb->kfifo_ts); |
Bryan O'Donoghue | cb60f49 | 2015-07-28 18:34:39 +0100 | [diff] [blame] | 266 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 267 | switch (gb->type) { |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 268 | case GB_LOOPBACK_TYPE_PING: |
| 269 | case GB_LOOPBACK_TYPE_TRANSFER: |
| 270 | case GB_LOOPBACK_TYPE_SINK: |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 271 | 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'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 276 | gb_loopback_reset_stats(gb); |
| 277 | wake_up(&gb->wq); |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 278 | break; |
| 279 | default: |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 280 | gb->type = 0; |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 281 | break; |
| 282 | } |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Time to send and receive one message */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 286 | gb_loopback_stats_attrs(latency); |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 287 | /* Number of requests sent per second on this cport */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 288 | gb_loopback_stats_attrs(requests_per_second); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 289 | /* Quantity of data sent and received on this cport */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 290 | gb_loopback_stats_attrs(throughput); |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 291 | /* Latency across the UniPro link from APBridge's perspective */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 292 | gb_loopback_stats_attrs(apbridge_unipro_latency); |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 293 | /* Firmware induced overhead in the GPBridge */ |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 294 | gb_loopback_stats_attrs(gbphy_firmware_latency); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 295 | |
Bryan O'Donoghue | e140c75 | 2015-07-21 23:50:09 +0100 | [diff] [blame] | 296 | /* Number of errors encountered during loop */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 297 | gb_loopback_ro_attr(error); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 298 | /* Number of requests successfully completed async */ |
| 299 | gb_loopback_ro_attr(requests_completed); |
| 300 | /* Number of requests timed out async */ |
| 301 | gb_loopback_ro_attr(requests_timedout); |
| 302 | /* Timeout minimum in useconds */ |
| 303 | gb_loopback_ro_attr(timeout_min); |
| 304 | /* Timeout minimum in useconds */ |
| 305 | gb_loopback_ro_attr(timeout_max); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 306 | |
| 307 | /* |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 308 | * Type of loopback message to send based on protocol type definitions |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 309 | * 0 => Don't send message |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 310 | * 2 => Send ping message continuously (message without payload) |
Alex Elder | 006335a | 2015-08-03 12:57:10 -0500 | [diff] [blame] | 311 | * 3 => Send transfer message continuously (message with payload, |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 312 | * payload returned in response) |
| 313 | * 4 => Send a sink message (message with payload, no payload in response) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 314 | */ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 315 | gb_dev_loopback_rw_attr(type, d); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 316 | /* Size of transfer message payload: 0-4096 bytes */ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 317 | gb_dev_loopback_rw_attr(size, u); |
Alex Elder | 48f19ee | 2015-05-11 21:16:36 -0500 | [diff] [blame] | 318 | /* Time to wait between two messages: 0-1000 ms */ |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 319 | gb_dev_loopback_rw_attr(us_wait, d); |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 320 | /* Maximum iterations for a given operation: 1-(2^32-1), 0 implies infinite */ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 321 | gb_dev_loopback_rw_attr(iteration_max, u); |
| 322 | /* The current index of the for (i = 0; i < iteration_max; i++) loop */ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 323 | gb_dev_loopback_ro_attr(iteration_count, false); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 324 | /* A flag to indicate synchronous or asynchronous operations */ |
| 325 | gb_dev_loopback_rw_attr(async, u); |
| 326 | /* Timeout of an individual asynchronous request */ |
| 327 | gb_dev_loopback_rw_attr(timeout, u); |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 328 | /* Maximum number of in-flight operations before back-off */ |
| 329 | gb_dev_loopback_rw_attr(outstanding_operations_max, u); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 330 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 331 | static 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 Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 344 | &dev_attr_gbphy_firmware_latency_min.attr, |
| 345 | &dev_attr_gbphy_firmware_latency_max.attr, |
| 346 | &dev_attr_gbphy_firmware_latency_avg.attr, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 347 | &dev_attr_type.attr, |
| 348 | &dev_attr_size.attr, |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 349 | &dev_attr_us_wait.attr, |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 350 | &dev_attr_iteration_count.attr, |
| 351 | &dev_attr_iteration_max.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 352 | &dev_attr_async.attr, |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 353 | &dev_attr_error.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 354 | &dev_attr_requests_completed.attr, |
| 355 | &dev_attr_requests_timedout.attr, |
| 356 | &dev_attr_timeout.attr, |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 357 | &dev_attr_outstanding_operations_max.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 358 | &dev_attr_timeout_min.attr, |
| 359 | &dev_attr_timeout_max.attr, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 360 | NULL, |
| 361 | }; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 362 | ATTRIBUTE_GROUPS(loopback); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 363 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 364 | static void gb_loopback_calculate_stats(struct gb_loopback *gb, bool error); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 365 | |
Bryan O'Donoghue | bd41610 | 2015-08-17 00:55:05 +0100 | [diff] [blame] | 366 | static 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'Donoghue | 7c98535 | 2015-08-17 00:55:06 +0100 | [diff] [blame] | 375 | static 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'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 383 | static u64 gb_loopback_calc_latency(struct timeval *ts, struct timeval *te) |
Bryan O'Donoghue | 4c19266 | 2015-08-11 13:50:53 +0100 | [diff] [blame] | 384 | { |
| 385 | u64 t1, t2; |
| 386 | |
| 387 | t1 = timeval_to_ns(ts); |
| 388 | t2 = timeval_to_ns(te); |
Bryan O'Donoghue | 7c98535 | 2015-08-17 00:55:06 +0100 | [diff] [blame] | 389 | |
| 390 | return __gb_loopback_calc_latency(t1, t2); |
Bryan O'Donoghue | 4c19266 | 2015-08-11 13:50:53 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 393 | static 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'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 400 | static 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'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 403 | { |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 404 | struct gb_operation *operation; |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 405 | struct timeval ts, te; |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 406 | int ret; |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 407 | |
| 408 | do_gettimeofday(&ts); |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 409 | operation = gb_operation_create(gb->connection, type, request_size, |
| 410 | response_size, GFP_KERNEL); |
Alexandre Bailon | d9048d8 | 2016-02-25 18:19:14 +0100 | [diff] [blame] | 411 | if (!operation) |
| 412 | return -ENOMEM; |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 413 | |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 414 | 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-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 419 | dev_err(&gb->connection->bundle->dev, |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 420 | "synchronous operation failed: %d\n", ret); |
Alexandre Bailon | d9048d8 | 2016-02-25 18:19:14 +0100 | [diff] [blame] | 421 | goto out_put_operation; |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 422 | } else { |
| 423 | if (response_size == operation->response->payload_size) { |
| 424 | memcpy(response, operation->response->payload, |
| 425 | response_size); |
| 426 | } else { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 427 | dev_err(&gb->connection->bundle->dev, |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 428 | "response size %zu expected %d\n", |
| 429 | operation->response->payload_size, |
| 430 | response_size); |
Alexandre Bailon | d9048d8 | 2016-02-25 18:19:14 +0100 | [diff] [blame] | 431 | ret = -EINVAL; |
| 432 | goto out_put_operation; |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 433 | } |
| 434 | } |
Johan Hovold | 6ab1ce4 | 2015-09-26 17:59:15 -0700 | [diff] [blame] | 435 | |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 436 | do_gettimeofday(&te); |
Bryan O'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 437 | |
| 438 | /* Calculate the total time the message took */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 439 | gb_loopback_push_latency_ts(gb, &ts, &te); |
Bryan O'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 440 | gb->elapsed_nsecs = gb_loopback_calc_latency(&ts, &te); |
| 441 | |
Alexandre Bailon | d9048d8 | 2016-02-25 18:19:14 +0100 | [diff] [blame] | 442 | out_put_operation: |
| 443 | gb_operation_put(operation); |
| 444 | |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 445 | return ret; |
| 446 | } |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 447 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 448 | static 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'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 457 | atomic_dec(&op_async->gb->outstanding_operations); |
| 458 | wake_up(&op_async->gb->wq_completion); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 459 | kfree(op_async); |
| 460 | } |
| 461 | |
| 462 | static void gb_loopback_async_operation_get(struct gb_loopback_async_operation |
| 463 | *op_async) |
| 464 | { |
| 465 | kref_get(&op_async->kref); |
| 466 | } |
| 467 | |
| 468 | static 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 | |
| 478 | static 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'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 498 | static 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'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 504 | static 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 Hovold | 41993cd | 2016-03-03 15:29:39 +0100 | [diff] [blame] | 527 | if (!err) { |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 528 | 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'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (op_async->pending) { |
Johan Hovold | 41993cd | 2016-03-03 15:29:39 +0100 | [diff] [blame] | 534 | if (err) |
| 535 | gb->error++; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 536 | gb->iteration_count++; |
| 537 | op_async->pending = false; |
| 538 | del_timer_sync(&op_async->timer); |
| 539 | gb_loopback_async_operation_put(op_async); |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 540 | gb_loopback_calculate_stats(gb, err); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 541 | } |
| 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 | |
| 550 | static 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'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 557 | 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 Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 567 | gb_loopback_calculate_stats(gb, true); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 568 | } |
| 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 | |
| 578 | static 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 | |
| 591 | static 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'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 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) { |
Bryan O'Donoghue | c7aae4e | 2015-12-11 13:46:51 +0000 | [diff] [blame] | 611 | kfree(op_async); |
| 612 | return -ENOMEM; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 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; |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 628 | atomic_inc(&gb->outstanding_operations); |
Bryan O'Donoghue | fece9c8 | 2016-03-16 11:29:59 +0000 | [diff] [blame] | 629 | mutex_lock(&gb->mutex); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 630 | ret = gb_operation_request_send(operation, |
| 631 | gb_loopback_async_operation_callback, |
| 632 | GFP_KERNEL); |
| 633 | if (ret) |
| 634 | goto error; |
| 635 | |
sayli karnik | 82af03f | 2016-09-22 22:09:06 +0530 | [diff] [blame] | 636 | setup_timer(&op_async->timer, gb_loopback_async_operation_timeout, |
| 637 | (unsigned long)operation->id); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 638 | op_async->timer.expires = jiffies + gb->jiffy_timeout; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 639 | add_timer(&op_async->timer); |
| 640 | |
Bryan O'Donoghue | fece9c8 | 2016-03-16 11:29:59 +0000 | [diff] [blame] | 641 | goto done; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 642 | error: |
| 643 | gb_loopback_async_operation_put(op_async); |
Bryan O'Donoghue | fece9c8 | 2016-03-16 11:29:59 +0000 | [diff] [blame] | 644 | done: |
| 645 | mutex_unlock(&gb->mutex); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static int gb_loopback_sync_sink(struct gb_loopback *gb, u32 len) |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 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); |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 662 | kfree(request); |
| 663 | return retval; |
| 664 | } |
| 665 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 666 | static int gb_loopback_sync_transfer(struct gb_loopback *gb, u32 len) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 667 | { |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 668 | struct gb_loopback_transfer_request *request; |
| 669 | struct gb_loopback_transfer_response *response; |
| 670 | int retval; |
| 671 | |
Bryan O'Donoghue | d6a1a3b | 2015-12-03 17:29:39 +0000 | [diff] [blame] | 672 | gb->apbridge_latency_ts = 0; |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 673 | gb->gbphy_latency_ts = 0; |
Bryan O'Donoghue | d6a1a3b | 2015-12-03 17:29:39 +0000 | [diff] [blame] | 674 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 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 | |
Viresh Kumar | dc4a106 | 2015-08-14 17:07:10 +0530 | [diff] [blame] | 684 | memset(request->data, 0x5A, len); |
| 685 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 686 | request->len = cpu_to_le32(len); |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 687 | retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_TRANSFER, |
| 688 | request, len + sizeof(*request), |
| 689 | response, len + sizeof(*response)); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 690 | if (retval) |
| 691 | goto gb_error; |
| 692 | |
Viresh Kumar | dc366f8 | 2015-08-14 17:07:11 +0530 | [diff] [blame] | 693 | if (memcmp(request->data, response->data, len)) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 694 | dev_err(&gb->connection->bundle->dev, |
| 695 | "Loopback Data doesn't match\n"); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 696 | retval = -EREMOTEIO; |
Viresh Kumar | dc366f8 | 2015-08-14 17:07:11 +0530 | [diff] [blame] | 697 | } |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 698 | gb->apbridge_latency_ts = (u32)__le32_to_cpu(response->reserved0); |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 699 | gb->gbphy_latency_ts = (u32)__le32_to_cpu(response->reserved1); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 700 | |
| 701 | gb_error: |
| 702 | kfree(request); |
| 703 | kfree(response); |
| 704 | |
| 705 | return retval; |
| 706 | } |
| 707 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 708 | static int gb_loopback_sync_ping(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 709 | { |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 710 | return gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_PING, |
| 711 | NULL, 0, NULL, 0); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 714 | static 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 | |
| 731 | static 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 Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 755 | gb->gbphy_latency_ts = |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 756 | (u32)__le32_to_cpu(response->reserved1); |
| 757 | } |
| 758 | |
| 759 | return retval; |
| 760 | } |
| 761 | |
| 762 | static 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 | |
| 782 | gb_error: |
| 783 | kfree(request); |
| 784 | return retval; |
| 785 | } |
| 786 | |
| 787 | static 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 Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 793 | static int gb_loopback_request_handler(struct gb_operation *operation) |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 794 | { |
| 795 | struct gb_connection *connection = operation->connection; |
| 796 | struct gb_loopback_transfer_request *request; |
| 797 | struct gb_loopback_transfer_response *response; |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 798 | struct device *dev = &connection->bundle->dev; |
Bryan O'Donoghue | e51eafe | 2015-07-14 00:53:13 +0100 | [diff] [blame] | 799 | size_t len; |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 800 | |
| 801 | /* By convention, the AP initiates the version operation */ |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 802 | switch (operation->type) { |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 803 | case GB_LOOPBACK_TYPE_PING: |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 804 | case GB_LOOPBACK_TYPE_SINK: |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 805 | return 0; |
| 806 | case GB_LOOPBACK_TYPE_TRANSFER: |
| 807 | if (operation->request->payload_size < sizeof(*request)) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 808 | dev_err(dev, "transfer request too small (%zu < %zu)\n", |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 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); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 815 | if (len > gb_dev.size_max) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 816 | dev_err(dev, "transfer request too large (%zu > %zu)\n", |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 817 | len, gb_dev.size_max); |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 818 | return -EINVAL; |
| 819 | } |
| 820 | |
Bartosz Golaszewski | 81ad699 | 2015-11-19 13:46:43 +0100 | [diff] [blame] | 821 | if (!gb_operation_response_alloc(operation, |
| 822 | len + sizeof(*response), GFP_KERNEL)) { |
| 823 | dev_err(dev, "error allocating response\n"); |
| 824 | return -ENOMEM; |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 825 | } |
Bartosz Golaszewski | 81ad699 | 2015-11-19 13:46:43 +0100 | [diff] [blame] | 826 | response = operation->response->payload; |
| 827 | response->len = cpu_to_le32(len); |
| 828 | if (len) |
| 829 | memcpy(response->data, request->data, len); |
| 830 | |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 831 | return 0; |
| 832 | default: |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 833 | dev_err(dev, "unsupported request: %u\n", operation->type); |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 834 | return -EINVAL; |
| 835 | } |
| 836 | } |
| 837 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 838 | static void gb_loopback_reset_stats(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 839 | { |
| 840 | struct gb_loopback_stats reset = { |
Alex Elder | e051c82 | 2015-08-03 12:57:14 -0500 | [diff] [blame] | 841 | .min = U32_MAX, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 842 | }; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 843 | |
| 844 | /* Reset per-connection stats */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 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)); |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 853 | memcpy(&gb->gbphy_firmware_latency, &reset, |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 854 | sizeof(struct gb_loopback_stats)); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 855 | |
Bryan O'Donoghue | f42a689 | 2015-12-07 01:59:08 +0000 | [diff] [blame] | 856 | /* Should be initialized at least once per transaction set */ |
| 857 | gb->apbridge_latency_ts = 0; |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 858 | gb->gbphy_latency_ts = 0; |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 859 | memset(&gb->ts, 0, sizeof(struct timeval)); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Alex Elder | a6e7e53 | 2015-08-03 12:57:13 -0500 | [diff] [blame] | 862 | static void gb_loopback_update_stats(struct gb_loopback_stats *stats, u32 val) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 863 | { |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 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++; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 870 | } |
| 871 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 872 | static 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'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 885 | static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 886 | { |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 887 | u64 req = gb->requests_completed * USEC_PER_SEC; |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 888 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 889 | gb_loopback_update_stats_window(&gb->requests_per_second, req, latency); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 892 | static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 893 | { |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 894 | u64 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 895 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 896 | switch (gb->type) { |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 897 | case GB_LOOPBACK_TYPE_PING: |
| 898 | break; |
| 899 | case GB_LOOPBACK_TYPE_SINK: |
| 900 | aggregate_size += sizeof(struct gb_loopback_transfer_request) + |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 901 | gb->size; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 902 | 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'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 906 | gb->size * 2; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 907 | break; |
| 908 | default: |
| 909 | return; |
| 910 | } |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 911 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 912 | aggregate_size *= gb->requests_completed; |
| 913 | aggregate_size *= USEC_PER_SEC; |
| 914 | gb_loopback_update_stats_window(&gb->throughput, aggregate_size, |
| 915 | latency); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 918 | static void gb_loopback_calculate_latency_stats(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 919 | { |
| 920 | u32 lat; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 921 | |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 922 | /* Express latency in terms of microseconds */ |
Bryan O'Donoghue | bd41610 | 2015-08-17 00:55:05 +0100 | [diff] [blame] | 923 | lat = gb_loopback_nsec_to_usec_latency(gb->elapsed_nsecs); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 924 | |
Bryan O'Donoghue | 98676ca | 2015-08-17 00:55:12 +0100 | [diff] [blame] | 925 | /* Log latency stastic */ |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 926 | gb_loopback_update_stats(&gb->latency, lat); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 927 | |
| 928 | /* Raw latency log on a per thread basis */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 929 | kfifo_in(&gb->kfifo_lat, (unsigned char *)&lat, sizeof(lat)); |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 930 | |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 931 | /* Log the firmware supplied latency values */ |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 932 | gb_loopback_update_stats(&gb->apbridge_unipro_latency, |
| 933 | gb->apbridge_latency_ts); |
Sandeep Patil | e54b106 | 2016-05-19 08:52:39 -0700 | [diff] [blame] | 934 | gb_loopback_update_stats(&gb->gbphy_firmware_latency, |
| 935 | gb->gbphy_latency_ts); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 938 | static 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'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 964 | static 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 Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 974 | static int gb_loopback_fn(void *data) |
| 975 | { |
| 976 | int error = 0; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 977 | int us_wait = 0; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 978 | int type; |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 979 | int ret; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 980 | u32 size; |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 981 | |
Alex Elder | 3f12c3e | 2015-08-03 12:57:11 -0500 | [diff] [blame] | 982 | struct gb_loopback *gb = data; |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 983 | struct gb_bundle *bundle = gb->connection->bundle; |
| 984 | |
| 985 | ret = gb_pm_runtime_get_sync(bundle); |
| 986 | if (ret) |
| 987 | return ret; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 988 | |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 989 | while (1) { |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 990 | if (!gb->type) { |
| 991 | gb_pm_runtime_put_autosuspend(bundle); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 992 | wait_event_interruptible(gb->wq, gb->type || |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 993 | kthread_should_stop()); |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 994 | ret = gb_pm_runtime_get_sync(bundle); |
| 995 | if (ret) |
| 996 | return ret; |
| 997 | } |
| 998 | |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 999 | if (kthread_should_stop()) |
| 1000 | break; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1001 | |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 1002 | /* Limit the maximum number of in-flight async operations */ |
| 1003 | gb_loopback_async_wait_to_send(gb); |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 1004 | if (kthread_should_stop()) |
| 1005 | break; |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 1006 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1007 | mutex_lock(&gb->mutex); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1008 | |
| 1009 | /* Optionally terminate */ |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 1010 | if (gb->send_count == gb->iteration_max) { |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 1011 | if (gb->iteration_count == gb->iteration_max) { |
| 1012 | gb->type = 0; |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 1013 | gb->send_count = 0; |
Axel Haslam | 39c2787 | 2016-02-26 11:39:48 +0100 | [diff] [blame] | 1014 | sysfs_notify(&gb->dev->kobj, NULL, |
| 1015 | "iteration_count"); |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 1016 | } |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1017 | mutex_unlock(&gb->mutex); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1018 | continue; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1019 | } |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1020 | size = gb->size; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 1021 | us_wait = gb->us_wait; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1022 | type = gb->type; |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 1023 | if (gb->ts.tv_usec == 0 && gb->ts.tv_sec == 0) |
| 1024 | do_gettimeofday(&gb->ts); |
Bryan O'Donoghue | d9fb375 | 2015-12-03 17:29:38 +0000 | [diff] [blame] | 1025 | mutex_unlock(&gb->mutex); |
| 1026 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1027 | /* Else operations to perform */ |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1028 | if (gb->async) { |
| 1029 | if (type == GB_LOOPBACK_TYPE_PING) { |
| 1030 | error = gb_loopback_async_ping(gb); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1031 | } 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'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1036 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1037 | 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'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1047 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1048 | if (error) |
| 1049 | gb->error++; |
| 1050 | gb->iteration_count++; |
Alexandre Bailon | ab81bb9 | 2016-02-25 18:19:15 +0100 | [diff] [blame] | 1051 | gb_loopback_calculate_stats(gb, !!error); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1052 | } |
Alexandre Bailon | 01480ba3 | 2016-03-08 17:40:16 +0100 | [diff] [blame] | 1053 | gb->send_count++; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 1054 | if (us_wait) |
| 1055 | udelay(us_wait); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1056 | } |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1057 | |
| 1058 | gb_pm_runtime_put_autosuspend(bundle); |
| 1059 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
| 1062 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1063 | static int gb_loopback_dbgfs_latency_show_common(struct seq_file *s, |
| 1064 | struct kfifo *kfifo, |
| 1065 | struct mutex *mutex) |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1066 | { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1067 | u32 latency; |
| 1068 | int retval; |
| 1069 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1070 | if (kfifo_len(kfifo) == 0) { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1071 | retval = -EAGAIN; |
| 1072 | goto done; |
| 1073 | } |
| 1074 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1075 | mutex_lock(mutex); |
| 1076 | retval = kfifo_out(kfifo, &latency, sizeof(latency)); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1077 | if (retval > 0) { |
| 1078 | seq_printf(s, "%u", latency); |
| 1079 | retval = 0; |
| 1080 | } |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1081 | mutex_unlock(mutex); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1082 | done: |
| 1083 | return retval; |
| 1084 | } |
| 1085 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1086 | static 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'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1094 | static 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 | |
| 1100 | static 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'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1107 | static 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'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1115 | 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 | |
| 1131 | static 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'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1145 | #define DEBUGFS_NAMELEN 32 |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1146 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1147 | static int gb_loopback_probe(struct gb_bundle *bundle, |
| 1148 | const struct greybus_bundle_id *id) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1149 | { |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1150 | struct greybus_descriptor_cport *cport_desc; |
| 1151 | struct gb_connection *connection; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1152 | struct gb_loopback *gb; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1153 | struct device *dev; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1154 | int retval; |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1155 | char name[DEBUGFS_NAMELEN]; |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1156 | unsigned long flags; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1157 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1158 | 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 Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1165 | gb = kzalloc(sizeof(*gb), GFP_KERNEL); |
| 1166 | if (!gb) |
| 1167 | return -ENOMEM; |
| 1168 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1169 | 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'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1179 | init_waitqueue_head(&gb->wq); |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 1180 | init_waitqueue_head(&gb->wq_completion); |
| 1181 | atomic_set(&gb->outstanding_operations, 0); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1182 | gb_loopback_reset_stats(gb); |
| 1183 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1184 | /* 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'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1188 | if (!gb_dev.count) { |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1189 | /* 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 Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1194 | goto out_connection_destroy; |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1195 | } |
| 1196 | gb_dev.size_max -= sizeof(struct gb_loopback_transfer_request); |
| 1197 | } |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* Create per-connection sysfs and debugfs data-points */ |
Bryan O'Donoghue | 8d8d36d | 2015-09-23 09:31:33 -0700 | [diff] [blame] | 1200 | snprintf(name, sizeof(name), "raw_latency_%s", |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 1201 | dev_name(&connection->bundle->dev)); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1202 | gb->file = debugfs_create_file(name, S_IFREG | S_IRUGO, gb_dev.root, gb, |
| 1203 | &gb_loopback_debugfs_latency_ops); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1204 | |
| 1205 | gb->id = ida_simple_get(&loopback_ida, 0, 0, GFP_KERNEL); |
| 1206 | if (gb->id < 0) { |
| 1207 | retval = gb->id; |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1208 | goto out_debugfs_remove; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1211 | retval = gb_connection_enable(connection); |
| 1212 | if (retval) |
| 1213 | goto out_ida_remove; |
| 1214 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1215 | 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 Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1221 | goto out_connection_disable; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1222 | } |
| 1223 | gb->dev = dev; |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 1224 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1225 | /* Allocate kfifo */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1226 | if (kfifo_alloc(&gb->kfifo_lat, kfifo_depth * sizeof(u32), |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1227 | GFP_KERNEL)) { |
| 1228 | retval = -ENOMEM; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1229 | goto out_conn; |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1230 | } |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1231 | 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'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1236 | |
| 1237 | /* Fork worker thread */ |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 1238 | mutex_init(&gb->mutex); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1239 | gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback"); |
| 1240 | if (IS_ERR(gb->task)) { |
Alex Elder | 69f6034 | 2015-05-11 21:16:35 -0500 | [diff] [blame] | 1241 | retval = PTR_ERR(gb->task); |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1242 | goto out_kfifo1; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1245 | spin_lock_irqsave(&gb_dev.lock, flags); |
Bryan O'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1246 | gb_loopback_insert_id(gb); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1247 | gb_dev.count++; |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1248 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 1249 | |
| 1250 | gb_connection_latency_tag_enable(connection); |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1251 | |
| 1252 | gb_pm_runtime_put_autosuspend(bundle); |
| 1253 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1254 | return 0; |
| 1255 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1256 | out_kfifo1: |
| 1257 | kfifo_free(&gb->kfifo_ts); |
| 1258 | out_kfifo0: |
| 1259 | kfifo_free(&gb->kfifo_lat); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1260 | out_conn: |
| 1261 | device_unregister(dev); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1262 | out_connection_disable: |
| 1263 | gb_connection_disable(connection); |
| 1264 | out_ida_remove: |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1265 | ida_simple_remove(&loopback_ida, gb->id); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1266 | out_debugfs_remove: |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1267 | debugfs_remove(gb->file); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1268 | out_connection_destroy: |
| 1269 | gb_connection_destroy(connection); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1270 | out_kzalloc: |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1271 | kfree(gb); |
Alex Elder | 1fb807c | 2015-08-03 12:57:20 -0500 | [diff] [blame] | 1272 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1273 | return retval; |
| 1274 | } |
| 1275 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1276 | static void gb_loopback_disconnect(struct gb_bundle *bundle) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1277 | { |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1278 | struct gb_loopback *gb = greybus_get_drvdata(bundle); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1279 | unsigned long flags; |
Axel Haslam | e854ff5 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 1280 | int ret; |
| 1281 | |
| 1282 | ret = gb_pm_runtime_get_sync(bundle); |
| 1283 | if (ret) |
| 1284 | gb_pm_runtime_get_noresume(bundle); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1285 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1286 | gb_connection_disable(gb->connection); |
| 1287 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1288 | if (!IS_ERR_OR_NULL(gb->task)) |
| 1289 | kthread_stop(gb->task); |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1290 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1291 | kfifo_free(&gb->kfifo_lat); |
| 1292 | kfifo_free(&gb->kfifo_ts); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1293 | gb_connection_latency_tag_disable(gb->connection); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1294 | debugfs_remove(gb->file); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1295 | |
| 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'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 1301 | gb_loopback_async_wait_all(gb); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1302 | |
| 1303 | spin_lock_irqsave(&gb_dev.lock, flags); |
| 1304 | gb_dev.count--; |
Bryan O'Donoghue | ff477d0 | 2015-09-04 16:53:31 +0100 | [diff] [blame] | 1305 | list_del(&gb->entry); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1306 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 1307 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1308 | device_unregister(gb->dev); |
| 1309 | ida_simple_remove(&loopback_ida, gb->id); |
| 1310 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1311 | gb_connection_destroy(gb->connection); |
Bryan O'Donoghue | 5f3e0d1 | 2015-09-14 10:48:41 +0100 | [diff] [blame] | 1312 | kfree(gb); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1315 | static const struct greybus_bundle_id gb_loopback_id_table[] = { |
| 1316 | { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOOPBACK) }, |
| 1317 | { } |
| 1318 | }; |
| 1319 | MODULE_DEVICE_TABLE(greybus, gb_loopback_id_table); |
| 1320 | |
| 1321 | static 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 Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1326 | }; |
| 1327 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1328 | static int loopback_init(void) |
| 1329 | { |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1330 | int retval; |
| 1331 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1332 | INIT_LIST_HEAD(&gb_dev.list); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1333 | INIT_LIST_HEAD(&gb_dev.list_op_async); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1334 | spin_lock_init(&gb_dev.lock); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1335 | gb_dev.root = debugfs_create_dir("gb_loopback", NULL); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1336 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1337 | retval = class_register(&loopback_class); |
| 1338 | if (retval) |
| 1339 | goto err; |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1340 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1341 | retval = greybus_register(&gb_loopback_driver); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1342 | if (retval) |
| 1343 | goto err_unregister; |
| 1344 | |
| 1345 | return 0; |
| 1346 | |
| 1347 | err_unregister: |
| 1348 | class_unregister(&loopback_class); |
| 1349 | err: |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1350 | debugfs_remove_recursive(gb_dev.root); |
| 1351 | return retval; |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1352 | } |
| 1353 | module_init(loopback_init); |
| 1354 | |
| 1355 | static void __exit loopback_exit(void) |
| 1356 | { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1357 | debugfs_remove_recursive(gb_dev.root); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1358 | greybus_deregister(&gb_loopback_driver); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1359 | class_unregister(&loopback_class); |
| 1360 | ida_destroy(&loopback_ida); |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1361 | } |
| 1362 | module_exit(loopback_exit); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1363 | |
| 1364 | MODULE_LICENSE("GPL v2"); |