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> |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 28 | |
Johan Hovold | 1ffc12b | 2015-04-16 09:53:59 +0200 | [diff] [blame] | 29 | #include <asm/div64.h> |
| 30 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 31 | #include "greybus.h" |
Bryan O'Donoghue | fd58926 | 2015-10-15 16:10:43 +0100 | [diff] [blame] | 32 | #include "connection.h" |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 33 | |
Bryan O'Donoghue | fd489e1 | 2015-08-11 13:50:54 +0100 | [diff] [blame] | 34 | #define NSEC_PER_DAY 86400000000000ULL |
| 35 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 36 | struct gb_loopback_stats { |
Alex Elder | e051c82 | 2015-08-03 12:57:14 -0500 | [diff] [blame] | 37 | u32 min; |
| 38 | u32 max; |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 39 | u64 sum; |
Alex Elder | e13fc28 | 2015-08-03 12:57:15 -0500 | [diff] [blame] | 40 | u32 count; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 43 | struct gb_loopback_device { |
| 44 | struct dentry *root; |
| 45 | u32 count; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 46 | size_t size_max; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 47 | |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 48 | /* We need to take a lock in atomic context */ |
| 49 | spinlock_t lock; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 50 | struct list_head list; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 51 | struct list_head list_op_async; |
| 52 | wait_queue_head_t wq; |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static struct gb_loopback_device gb_dev; |
| 56 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 57 | struct 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 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 69 | struct gb_loopback { |
| 70 | struct gb_connection *connection; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 71 | |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 72 | struct dentry *file; |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 73 | struct kfifo kfifo_lat; |
| 74 | struct kfifo kfifo_ts; |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 75 | struct mutex mutex; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 76 | struct task_struct *task; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 77 | struct list_head entry; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 78 | struct device *dev; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 79 | wait_queue_head_t wq; |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 80 | wait_queue_head_t wq_completion; |
| 81 | atomic_t outstanding_operations; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 82 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 83 | /* Per connection stats */ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 84 | struct gb_loopback_stats latency; |
| 85 | struct gb_loopback_stats throughput; |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 86 | struct gb_loopback_stats requests_per_second; |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 87 | struct gb_loopback_stats apbridge_unipro_latency; |
| 88 | struct gb_loopback_stats gpbridge_firmware_latency; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 89 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 90 | int type; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 91 | int async; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 92 | int id; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 93 | u32 size; |
| 94 | u32 iteration_max; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 95 | u32 iteration_count; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 96 | int us_wait; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 97 | u32 error; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 98 | u32 requests_completed; |
| 99 | u32 requests_timedout; |
| 100 | u32 timeout; |
| 101 | u32 jiffy_timeout; |
| 102 | u32 timeout_min; |
| 103 | u32 timeout_max; |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 104 | u32 outstanding_operations_max; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 105 | u32 lbid; |
| 106 | u64 elapsed_nsecs; |
Bryan O'Donoghue | e6227ee | 2015-10-15 16:10:44 +0100 | [diff] [blame] | 107 | u32 apbridge_latency_ts; |
| 108 | u32 gpbridge_latency_ts; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 111 | static struct class loopback_class = { |
| 112 | .name = "gb_loopback", |
| 113 | .owner = THIS_MODULE, |
| 114 | }; |
| 115 | static DEFINE_IDA(loopback_ida); |
| 116 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 117 | /* Min/max values in jiffies */ |
| 118 | #define GB_LOOPBACK_TIMEOUT_MIN 1 |
| 119 | #define GB_LOOPBACK_TIMEOUT_MAX 10000 |
| 120 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 121 | #define GB_LOOPBACK_FIFO_DEFAULT 8192 |
| 122 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 123 | static unsigned kfifo_depth = GB_LOOPBACK_FIFO_DEFAULT; |
| 124 | module_param(kfifo_depth, uint, 0444); |
| 125 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 126 | /* Maximum size of any one send data buffer we support */ |
| 127 | #define MAX_PACKET_SIZE (PAGE_SIZE * 2) |
| 128 | |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 129 | #define GB_LOOPBACK_US_WAIT_MAX 1000000 |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 130 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 131 | /* interface sysfs attributes */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 132 | #define gb_loopback_ro_attr(field) \ |
| 133 | static ssize_t field##_show(struct device *dev, \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 134 | struct device_attribute *attr, \ |
| 135 | char *buf) \ |
| 136 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 137 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 138 | return sprintf(buf, "%u\n", gb->field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 139 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 140 | static DEVICE_ATTR_RO(field) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 141 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 142 | #define gb_loopback_ro_stats_attr(name, field, type) \ |
| 143 | static ssize_t name##_##field##_show(struct device *dev, \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 144 | struct device_attribute *attr, \ |
| 145 | char *buf) \ |
| 146 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 147 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 148 | return sprintf(buf, "%"#type"\n", gb->name.field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 149 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 150 | static DEVICE_ATTR_RO(name##_##field) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 151 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 152 | #define gb_loopback_ro_avg_attr(name) \ |
| 153 | static ssize_t name##_avg_show(struct device *dev, \ |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 154 | struct device_attribute *attr, \ |
| 155 | char *buf) \ |
| 156 | { \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 157 | struct gb_loopback_stats *stats; \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 158 | struct gb_loopback *gb; \ |
| 159 | u64 avg; \ |
| 160 | u32 count, rem; \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 161 | gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 162 | stats = &gb->name; \ |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 163 | count = stats->count ? stats->count : 1; \ |
| 164 | avg = stats->sum + count / 2; /* round closest */ \ |
| 165 | rem = do_div(avg, count); \ |
Alex Elder | ff71d39 | 2015-08-03 12:57:19 -0500 | [diff] [blame] | 166 | return sprintf(buf, "%llu.%06u\n", avg, 1000000 * rem / count); \ |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 167 | } \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 168 | static DEVICE_ATTR_RO(name##_avg) |
Alex Elder | 7a135a9 | 2015-08-03 12:57:18 -0500 | [diff] [blame] | 169 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 170 | #define gb_loopback_stats_attrs(field) \ |
| 171 | gb_loopback_ro_stats_attr(field, min, u); \ |
| 172 | gb_loopback_ro_stats_attr(field, max, u); \ |
| 173 | gb_loopback_ro_avg_attr(field) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 174 | |
| 175 | #define gb_loopback_attr(field, type) \ |
| 176 | static ssize_t field##_show(struct device *dev, \ |
| 177 | struct device_attribute *attr, \ |
| 178 | char *buf) \ |
| 179 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 180 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 181 | return sprintf(buf, "%"#type"\n", gb->field); \ |
| 182 | } \ |
| 183 | static ssize_t field##_store(struct device *dev, \ |
| 184 | struct device_attribute *attr, \ |
| 185 | const char *buf, \ |
| 186 | size_t len) \ |
| 187 | { \ |
| 188 | int ret; \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 189 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 190 | mutex_lock(&gb->mutex); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 191 | ret = sscanf(buf, "%"#type, &gb->field); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 192 | if (ret != 1) \ |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 193 | len = -EINVAL; \ |
| 194 | else \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 195 | gb_loopback_check_attr(gb, bundle); \ |
| 196 | mutex_unlock(&gb->mutex); \ |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 197 | return len; \ |
| 198 | } \ |
| 199 | static DEVICE_ATTR_RW(field) |
| 200 | |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 201 | #define gb_dev_loopback_ro_attr(field, conn) \ |
| 202 | static ssize_t field##_show(struct device *dev, \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 203 | struct device_attribute *attr, \ |
| 204 | char *buf) \ |
| 205 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 206 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 207 | return sprintf(buf, "%u\n", gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 208 | } \ |
| 209 | static DEVICE_ATTR_RO(field) |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 210 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 211 | #define gb_dev_loopback_rw_attr(field, type) \ |
| 212 | static ssize_t field##_show(struct device *dev, \ |
| 213 | struct device_attribute *attr, \ |
| 214 | char *buf) \ |
| 215 | { \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 216 | struct gb_loopback *gb = dev_get_drvdata(dev); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 217 | return sprintf(buf, "%"#type"\n", gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 218 | } \ |
| 219 | static ssize_t field##_store(struct device *dev, \ |
| 220 | struct device_attribute *attr, \ |
| 221 | const char *buf, \ |
| 222 | size_t len) \ |
| 223 | { \ |
| 224 | int ret; \ |
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 | mutex_lock(&gb->mutex); \ |
| 227 | ret = sscanf(buf, "%"#type, &gb->field); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 228 | if (ret != 1) \ |
| 229 | len = -EINVAL; \ |
| 230 | else \ |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 231 | gb_loopback_check_attr(gb); \ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 232 | mutex_unlock(&gb->mutex); \ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 233 | return len; \ |
| 234 | } \ |
| 235 | static DEVICE_ATTR_RW(field) |
| 236 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 237 | static void gb_loopback_reset_stats(struct gb_loopback *gb); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 238 | static void gb_loopback_check_attr(struct gb_loopback *gb) |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 239 | { |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 240 | if (gb->us_wait > GB_LOOPBACK_US_WAIT_MAX) |
| 241 | gb->us_wait = GB_LOOPBACK_US_WAIT_MAX; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 242 | if (gb->size > gb_dev.size_max) |
| 243 | gb->size = gb_dev.size_max; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 244 | gb->requests_timedout = 0; |
| 245 | gb->requests_completed = 0; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 246 | gb->iteration_count = 0; |
| 247 | gb->error = 0; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 248 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 249 | if (kfifo_depth < gb->iteration_max) { |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 250 | dev_warn(gb->dev, |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 251 | "cannot log bytes %u kfifo_depth %u\n", |
| 252 | gb->iteration_max, kfifo_depth); |
Bryan O'Donoghue | cb60f49 | 2015-07-28 18:34:39 +0100 | [diff] [blame] | 253 | } |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 254 | kfifo_reset_out(&gb->kfifo_lat); |
| 255 | kfifo_reset_out(&gb->kfifo_ts); |
Bryan O'Donoghue | cb60f49 | 2015-07-28 18:34:39 +0100 | [diff] [blame] | 256 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 257 | switch (gb->type) { |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 258 | case GB_LOOPBACK_TYPE_PING: |
| 259 | case GB_LOOPBACK_TYPE_TRANSFER: |
| 260 | case GB_LOOPBACK_TYPE_SINK: |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 261 | gb->jiffy_timeout = usecs_to_jiffies(gb->timeout); |
| 262 | if (!gb->jiffy_timeout) |
| 263 | gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MIN; |
| 264 | else if (gb->jiffy_timeout > GB_LOOPBACK_TIMEOUT_MAX) |
| 265 | gb->jiffy_timeout = GB_LOOPBACK_TIMEOUT_MAX; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 266 | gb_loopback_reset_stats(gb); |
| 267 | wake_up(&gb->wq); |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 268 | break; |
| 269 | default: |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 270 | gb->type = 0; |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 271 | break; |
| 272 | } |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Time to send and receive one message */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 276 | gb_loopback_stats_attrs(latency); |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 277 | /* Number of requests sent per second on this cport */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 278 | gb_loopback_stats_attrs(requests_per_second); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 279 | /* Quantity of data sent and received on this cport */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 280 | gb_loopback_stats_attrs(throughput); |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 281 | /* Latency across the UniPro link from APBridge's perspective */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 282 | gb_loopback_stats_attrs(apbridge_unipro_latency); |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 283 | /* Firmware induced overhead in the GPBridge */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 284 | gb_loopback_stats_attrs(gpbridge_firmware_latency); |
| 285 | |
Bryan O'Donoghue | e140c75 | 2015-07-21 23:50:09 +0100 | [diff] [blame] | 286 | /* Number of errors encountered during loop */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 287 | gb_loopback_ro_attr(error); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 288 | /* Number of requests successfully completed async */ |
| 289 | gb_loopback_ro_attr(requests_completed); |
| 290 | /* Number of requests timed out async */ |
| 291 | gb_loopback_ro_attr(requests_timedout); |
| 292 | /* Timeout minimum in useconds */ |
| 293 | gb_loopback_ro_attr(timeout_min); |
| 294 | /* Timeout minimum in useconds */ |
| 295 | gb_loopback_ro_attr(timeout_max); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 296 | |
| 297 | /* |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 298 | * Type of loopback message to send based on protocol type definitions |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 299 | * 0 => Don't send message |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 300 | * 2 => Send ping message continuously (message without payload) |
Alex Elder | 006335a | 2015-08-03 12:57:10 -0500 | [diff] [blame] | 301 | * 3 => Send transfer message continuously (message with payload, |
Bryan O'Donoghue | 799a3f0 | 2015-07-21 23:50:07 +0100 | [diff] [blame] | 302 | * payload returned in response) |
| 303 | * 4 => Send a sink message (message with payload, no payload in response) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 304 | */ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 305 | gb_dev_loopback_rw_attr(type, d); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 306 | /* Size of transfer message payload: 0-4096 bytes */ |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 307 | gb_dev_loopback_rw_attr(size, u); |
Alex Elder | 48f19ee | 2015-05-11 21:16:36 -0500 | [diff] [blame] | 308 | /* Time to wait between two messages: 0-1000 ms */ |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 309 | gb_dev_loopback_rw_attr(us_wait, d); |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 310 | /* 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] | 311 | gb_dev_loopback_rw_attr(iteration_max, u); |
| 312 | /* 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] | 313 | gb_dev_loopback_ro_attr(iteration_count, false); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 314 | /* A flag to indicate synchronous or asynchronous operations */ |
| 315 | gb_dev_loopback_rw_attr(async, u); |
| 316 | /* Timeout of an individual asynchronous request */ |
| 317 | gb_dev_loopback_rw_attr(timeout, u); |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 318 | /* Maximum number of in-flight operations before back-off */ |
| 319 | gb_dev_loopback_rw_attr(outstanding_operations_max, u); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 320 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 321 | static struct attribute *loopback_attrs[] = { |
| 322 | &dev_attr_latency_min.attr, |
| 323 | &dev_attr_latency_max.attr, |
| 324 | &dev_attr_latency_avg.attr, |
| 325 | &dev_attr_requests_per_second_min.attr, |
| 326 | &dev_attr_requests_per_second_max.attr, |
| 327 | &dev_attr_requests_per_second_avg.attr, |
| 328 | &dev_attr_throughput_min.attr, |
| 329 | &dev_attr_throughput_max.attr, |
| 330 | &dev_attr_throughput_avg.attr, |
| 331 | &dev_attr_apbridge_unipro_latency_min.attr, |
| 332 | &dev_attr_apbridge_unipro_latency_max.attr, |
| 333 | &dev_attr_apbridge_unipro_latency_avg.attr, |
| 334 | &dev_attr_gpbridge_firmware_latency_min.attr, |
| 335 | &dev_attr_gpbridge_firmware_latency_max.attr, |
| 336 | &dev_attr_gpbridge_firmware_latency_avg.attr, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 337 | &dev_attr_type.attr, |
| 338 | &dev_attr_size.attr, |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 339 | &dev_attr_us_wait.attr, |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 340 | &dev_attr_iteration_count.attr, |
| 341 | &dev_attr_iteration_max.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 342 | &dev_attr_async.attr, |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 343 | &dev_attr_error.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 344 | &dev_attr_requests_completed.attr, |
| 345 | &dev_attr_requests_timedout.attr, |
| 346 | &dev_attr_timeout.attr, |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 347 | &dev_attr_outstanding_operations_max.attr, |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 348 | &dev_attr_timeout_min.attr, |
| 349 | &dev_attr_timeout_max.attr, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 350 | NULL, |
| 351 | }; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 352 | ATTRIBUTE_GROUPS(loopback); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 353 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 354 | static void gb_loopback_calculate_stats(struct gb_loopback *gb); |
| 355 | |
Bryan O'Donoghue | bd41610 | 2015-08-17 00:55:05 +0100 | [diff] [blame] | 356 | static u32 gb_loopback_nsec_to_usec_latency(u64 elapsed_nsecs) |
| 357 | { |
| 358 | u32 lat; |
| 359 | |
| 360 | do_div(elapsed_nsecs, NSEC_PER_USEC); |
| 361 | lat = elapsed_nsecs; |
| 362 | return lat; |
| 363 | } |
| 364 | |
Bryan O'Donoghue | 7c98535 | 2015-08-17 00:55:06 +0100 | [diff] [blame] | 365 | static u64 __gb_loopback_calc_latency(u64 t1, u64 t2) |
| 366 | { |
| 367 | if (t2 > t1) |
| 368 | return t2 - t1; |
| 369 | else |
| 370 | return NSEC_PER_DAY - t2 + t1; |
| 371 | } |
| 372 | |
Bryan O'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 373 | 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] | 374 | { |
| 375 | u64 t1, t2; |
| 376 | |
| 377 | t1 = timeval_to_ns(ts); |
| 378 | t2 = timeval_to_ns(te); |
Bryan O'Donoghue | 7c98535 | 2015-08-17 00:55:06 +0100 | [diff] [blame] | 379 | |
| 380 | return __gb_loopback_calc_latency(t1, t2); |
Bryan O'Donoghue | 4c19266 | 2015-08-11 13:50:53 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 383 | static void gb_loopback_push_latency_ts(struct gb_loopback *gb, |
| 384 | struct timeval *ts, struct timeval *te) |
| 385 | { |
| 386 | kfifo_in(&gb->kfifo_ts, (unsigned char *)ts, sizeof(*ts)); |
| 387 | kfifo_in(&gb->kfifo_ts, (unsigned char *)te, sizeof(*te)); |
| 388 | } |
| 389 | |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 390 | static int gb_loopback_operation_sync(struct gb_loopback *gb, int type, |
| 391 | void *request, int request_size, |
| 392 | void *response, int response_size) |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 393 | { |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 394 | struct gb_operation *operation; |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 395 | struct timeval ts, te; |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 396 | int ret; |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 397 | |
| 398 | do_gettimeofday(&ts); |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 399 | operation = gb_operation_create(gb->connection, type, request_size, |
| 400 | response_size, GFP_KERNEL); |
| 401 | if (!operation) { |
| 402 | ret = -ENOMEM; |
| 403 | goto error; |
| 404 | } |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 405 | |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 406 | if (request_size) |
| 407 | memcpy(operation->request->payload, request, request_size); |
| 408 | |
| 409 | ret = gb_operation_request_send_sync(operation); |
| 410 | if (ret) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 411 | dev_err(&gb->connection->bundle->dev, |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 412 | "synchronous operation failed: %d\n", ret); |
| 413 | } else { |
| 414 | if (response_size == operation->response->payload_size) { |
| 415 | memcpy(response, operation->response->payload, |
| 416 | response_size); |
| 417 | } else { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 418 | dev_err(&gb->connection->bundle->dev, |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 419 | "response size %zu expected %d\n", |
| 420 | operation->response->payload_size, |
| 421 | response_size); |
| 422 | } |
| 423 | } |
Johan Hovold | 6ab1ce4 | 2015-09-26 17:59:15 -0700 | [diff] [blame] | 424 | |
| 425 | gb_operation_put(operation); |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 426 | |
| 427 | error: |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 428 | do_gettimeofday(&te); |
Bryan O'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 429 | |
| 430 | /* Calculate the total time the message took */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 431 | gb_loopback_push_latency_ts(gb, &ts, &te); |
Bryan O'Donoghue | 2f84230 | 2015-08-17 00:55:02 +0100 | [diff] [blame] | 432 | gb->elapsed_nsecs = gb_loopback_calc_latency(&ts, &te); |
| 433 | |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 434 | return ret; |
| 435 | } |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 436 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 437 | static void __gb_loopback_async_operation_destroy(struct kref *kref) |
| 438 | { |
| 439 | struct gb_loopback_async_operation *op_async; |
| 440 | |
| 441 | op_async = container_of(kref, struct gb_loopback_async_operation, kref); |
| 442 | |
| 443 | list_del(&op_async->entry); |
| 444 | if (op_async->operation) |
| 445 | gb_operation_put(op_async->operation); |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 446 | atomic_dec(&op_async->gb->outstanding_operations); |
| 447 | wake_up(&op_async->gb->wq_completion); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 448 | kfree(op_async); |
| 449 | } |
| 450 | |
| 451 | static void gb_loopback_async_operation_get(struct gb_loopback_async_operation |
| 452 | *op_async) |
| 453 | { |
| 454 | kref_get(&op_async->kref); |
| 455 | } |
| 456 | |
| 457 | static void gb_loopback_async_operation_put(struct gb_loopback_async_operation |
| 458 | *op_async) |
| 459 | { |
| 460 | unsigned long flags; |
| 461 | |
| 462 | spin_lock_irqsave(&gb_dev.lock, flags); |
| 463 | kref_put(&op_async->kref, __gb_loopback_async_operation_destroy); |
| 464 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 465 | } |
| 466 | |
| 467 | static struct gb_loopback_async_operation * |
| 468 | gb_loopback_operation_find(u16 id) |
| 469 | { |
| 470 | struct gb_loopback_async_operation *op_async; |
| 471 | bool found = false; |
| 472 | unsigned long flags; |
| 473 | |
| 474 | spin_lock_irqsave(&gb_dev.lock, flags); |
| 475 | list_for_each_entry(op_async, &gb_dev.list_op_async, entry) { |
| 476 | if (op_async->operation->id == id) { |
| 477 | gb_loopback_async_operation_get(op_async); |
| 478 | found = true; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 483 | |
| 484 | return found ? op_async : NULL; |
| 485 | } |
| 486 | |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 487 | static void gb_loopback_async_wait_all(struct gb_loopback *gb) |
| 488 | { |
| 489 | wait_event(gb->wq_completion, |
| 490 | !atomic_read(&gb->outstanding_operations)); |
| 491 | } |
| 492 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 493 | static void gb_loopback_async_operation_callback(struct gb_operation *operation) |
| 494 | { |
| 495 | struct gb_loopback_async_operation *op_async; |
| 496 | struct gb_loopback *gb; |
| 497 | struct timeval te; |
| 498 | bool err = false; |
| 499 | |
| 500 | do_gettimeofday(&te); |
| 501 | op_async = gb_loopback_operation_find(operation->id); |
| 502 | if (!op_async) |
| 503 | return; |
| 504 | |
| 505 | gb = op_async->gb; |
| 506 | mutex_lock(&gb->mutex); |
| 507 | |
| 508 | if (!op_async->pending || gb_operation_result(operation)) { |
| 509 | err = true; |
| 510 | } else { |
| 511 | if (op_async->completion) |
| 512 | if (op_async->completion(op_async)) |
| 513 | err = true; |
| 514 | } |
| 515 | |
| 516 | if (err) { |
| 517 | gb->error++; |
| 518 | } else { |
| 519 | gb->requests_completed++; |
| 520 | gb_loopback_push_latency_ts(gb, &op_async->ts, &te); |
| 521 | gb->elapsed_nsecs = gb_loopback_calc_latency(&op_async->ts, |
| 522 | &te); |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 523 | gb_loopback_calculate_stats(gb); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | if (op_async->pending) { |
| 527 | gb->iteration_count++; |
| 528 | op_async->pending = false; |
| 529 | del_timer_sync(&op_async->timer); |
| 530 | gb_loopback_async_operation_put(op_async); |
| 531 | } |
| 532 | mutex_unlock(&gb->mutex); |
| 533 | |
| 534 | dev_dbg(&gb->connection->bundle->dev, "complete operation %d\n", |
| 535 | operation->id); |
| 536 | |
| 537 | gb_loopback_async_operation_put(op_async); |
| 538 | } |
| 539 | |
| 540 | static void gb_loopback_async_operation_work(struct work_struct *work) |
| 541 | { |
| 542 | struct gb_loopback *gb; |
| 543 | struct gb_operation *operation; |
| 544 | struct gb_loopback_async_operation *op_async; |
| 545 | |
| 546 | op_async = container_of(work, struct gb_loopback_async_operation, work); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 547 | gb = op_async->gb; |
| 548 | operation = op_async->operation; |
| 549 | |
| 550 | mutex_lock(&gb->mutex); |
| 551 | if (op_async->pending) { |
| 552 | gb->requests_timedout++; |
| 553 | gb->error++; |
| 554 | gb->iteration_count++; |
| 555 | op_async->pending = false; |
| 556 | gb_loopback_async_operation_put(op_async); |
| 557 | } |
| 558 | mutex_unlock(&gb->mutex); |
| 559 | |
| 560 | dev_dbg(&gb->connection->bundle->dev, "timeout operation %d\n", |
| 561 | operation->id); |
| 562 | |
| 563 | gb_operation_cancel(operation, -ETIMEDOUT); |
| 564 | gb_loopback_async_operation_put(op_async); |
| 565 | } |
| 566 | |
| 567 | static void gb_loopback_async_operation_timeout(unsigned long data) |
| 568 | { |
| 569 | struct gb_loopback_async_operation *op_async; |
| 570 | u16 id = data; |
| 571 | |
| 572 | op_async = gb_loopback_operation_find(id); |
| 573 | if (!op_async) { |
| 574 | pr_err("operation %d not found - time out ?\n", id); |
| 575 | return; |
| 576 | } |
| 577 | schedule_work(&op_async->work); |
| 578 | } |
| 579 | |
| 580 | static int gb_loopback_async_operation(struct gb_loopback *gb, int type, |
| 581 | void *request, int request_size, |
| 582 | int response_size, |
| 583 | void *completion) |
| 584 | { |
| 585 | struct gb_loopback_async_operation *op_async; |
| 586 | struct gb_operation *operation; |
| 587 | int ret; |
| 588 | unsigned long flags; |
| 589 | |
| 590 | op_async = kzalloc(sizeof(*op_async), GFP_KERNEL); |
| 591 | if (!op_async) |
| 592 | return -ENOMEM; |
| 593 | |
| 594 | INIT_WORK(&op_async->work, gb_loopback_async_operation_work); |
| 595 | init_timer(&op_async->timer); |
| 596 | kref_init(&op_async->kref); |
| 597 | |
| 598 | operation = gb_operation_create(gb->connection, type, request_size, |
| 599 | response_size, GFP_KERNEL); |
| 600 | if (!operation) { |
Bryan O'Donoghue | c7aae4e | 2015-12-11 13:46:51 +0000 | [diff] [blame] | 601 | kfree(op_async); |
| 602 | return -ENOMEM; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | if (request_size) |
| 606 | memcpy(operation->request->payload, request, request_size); |
| 607 | |
| 608 | op_async->gb = gb; |
| 609 | op_async->operation = operation; |
| 610 | op_async->completion = completion; |
| 611 | |
| 612 | spin_lock_irqsave(&gb_dev.lock, flags); |
| 613 | list_add_tail(&op_async->entry, &gb_dev.list_op_async); |
| 614 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 615 | |
| 616 | do_gettimeofday(&op_async->ts); |
| 617 | op_async->pending = true; |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 618 | atomic_inc(&gb->outstanding_operations); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 619 | ret = gb_operation_request_send(operation, |
| 620 | gb_loopback_async_operation_callback, |
| 621 | GFP_KERNEL); |
| 622 | if (ret) |
| 623 | goto error; |
| 624 | |
| 625 | op_async->timer.function = gb_loopback_async_operation_timeout; |
| 626 | op_async->timer.expires = jiffies + gb->jiffy_timeout; |
| 627 | op_async->timer.data = (unsigned long)operation->id; |
| 628 | add_timer(&op_async->timer); |
| 629 | |
| 630 | return ret; |
| 631 | error: |
| 632 | gb_loopback_async_operation_put(op_async); |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | 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] | 637 | { |
| 638 | struct gb_loopback_transfer_request *request; |
| 639 | int retval; |
| 640 | |
| 641 | request = kmalloc(len + sizeof(*request), GFP_KERNEL); |
| 642 | if (!request) |
| 643 | return -ENOMEM; |
| 644 | |
| 645 | request->len = cpu_to_le32(len); |
| 646 | retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_SINK, |
| 647 | request, len + sizeof(*request), |
| 648 | NULL, 0); |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 649 | kfree(request); |
| 650 | return retval; |
| 651 | } |
| 652 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 653 | static int gb_loopback_sync_transfer(struct gb_loopback *gb, u32 len) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 654 | { |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 655 | struct gb_loopback_transfer_request *request; |
| 656 | struct gb_loopback_transfer_response *response; |
| 657 | int retval; |
| 658 | |
Bryan O'Donoghue | d6a1a3b | 2015-12-03 17:29:39 +0000 | [diff] [blame] | 659 | gb->apbridge_latency_ts = 0; |
| 660 | gb->gpbridge_latency_ts = 0; |
| 661 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 662 | request = kmalloc(len + sizeof(*request), GFP_KERNEL); |
| 663 | if (!request) |
| 664 | return -ENOMEM; |
| 665 | response = kmalloc(len + sizeof(*response), GFP_KERNEL); |
| 666 | if (!response) { |
| 667 | kfree(request); |
| 668 | return -ENOMEM; |
| 669 | } |
| 670 | |
Viresh Kumar | dc4a106 | 2015-08-14 17:07:10 +0530 | [diff] [blame] | 671 | memset(request->data, 0x5A, len); |
| 672 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 673 | request->len = cpu_to_le32(len); |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 674 | retval = gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_TRANSFER, |
| 675 | request, len + sizeof(*request), |
| 676 | response, len + sizeof(*response)); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 677 | if (retval) |
| 678 | goto gb_error; |
| 679 | |
Viresh Kumar | dc366f8 | 2015-08-14 17:07:11 +0530 | [diff] [blame] | 680 | if (memcmp(request->data, response->data, len)) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 681 | dev_err(&gb->connection->bundle->dev, |
| 682 | "Loopback Data doesn't match\n"); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 683 | retval = -EREMOTEIO; |
Viresh Kumar | dc366f8 | 2015-08-14 17:07:11 +0530 | [diff] [blame] | 684 | } |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 685 | gb->apbridge_latency_ts = (u32)__le32_to_cpu(response->reserved0); |
| 686 | gb->gpbridge_latency_ts = (u32)__le32_to_cpu(response->reserved1); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 687 | |
| 688 | gb_error: |
| 689 | kfree(request); |
| 690 | kfree(response); |
| 691 | |
| 692 | return retval; |
| 693 | } |
| 694 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 695 | static int gb_loopback_sync_ping(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 696 | { |
Bryan O'Donoghue | fbb8edb | 2015-09-14 10:48:47 +0100 | [diff] [blame] | 697 | return gb_loopback_operation_sync(gb, GB_LOOPBACK_TYPE_PING, |
| 698 | NULL, 0, NULL, 0); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 701 | static int gb_loopback_async_sink(struct gb_loopback *gb, u32 len) |
| 702 | { |
| 703 | struct gb_loopback_transfer_request *request; |
| 704 | int retval; |
| 705 | |
| 706 | request = kmalloc(len + sizeof(*request), GFP_KERNEL); |
| 707 | if (!request) |
| 708 | return -ENOMEM; |
| 709 | |
| 710 | request->len = cpu_to_le32(len); |
| 711 | retval = gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_SINK, |
| 712 | request, len + sizeof(*request), |
| 713 | 0, NULL); |
| 714 | kfree(request); |
| 715 | return retval; |
| 716 | } |
| 717 | |
| 718 | static int gb_loopback_async_transfer_complete( |
| 719 | struct gb_loopback_async_operation *op_async) |
| 720 | { |
| 721 | struct gb_loopback *gb; |
| 722 | struct gb_operation *operation; |
| 723 | struct gb_loopback_transfer_request *request; |
| 724 | struct gb_loopback_transfer_response *response; |
| 725 | size_t len; |
| 726 | int retval = 0; |
| 727 | |
| 728 | gb = op_async->gb; |
| 729 | operation = op_async->operation; |
| 730 | request = operation->request->payload; |
| 731 | response = operation->response->payload; |
| 732 | len = le32_to_cpu(request->len); |
| 733 | |
| 734 | if (memcmp(request->data, response->data, len)) { |
| 735 | dev_err(&gb->connection->bundle->dev, |
| 736 | "Loopback Data doesn't match operation id %d\n", |
| 737 | operation->id); |
| 738 | retval = -EREMOTEIO; |
| 739 | } else { |
| 740 | gb->apbridge_latency_ts = |
| 741 | (u32)__le32_to_cpu(response->reserved0); |
| 742 | gb->gpbridge_latency_ts = |
| 743 | (u32)__le32_to_cpu(response->reserved1); |
| 744 | } |
| 745 | |
| 746 | return retval; |
| 747 | } |
| 748 | |
| 749 | static int gb_loopback_async_transfer(struct gb_loopback *gb, u32 len) |
| 750 | { |
| 751 | struct gb_loopback_transfer_request *request; |
| 752 | int retval, response_len; |
| 753 | |
| 754 | request = kmalloc(len + sizeof(*request), GFP_KERNEL); |
| 755 | if (!request) |
| 756 | return -ENOMEM; |
| 757 | |
| 758 | memset(request->data, 0x5A, len); |
| 759 | |
| 760 | request->len = cpu_to_le32(len); |
| 761 | response_len = sizeof(struct gb_loopback_transfer_response); |
| 762 | retval = gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_TRANSFER, |
| 763 | request, len + sizeof(*request), |
| 764 | len + response_len, |
| 765 | gb_loopback_async_transfer_complete); |
| 766 | if (retval) |
| 767 | goto gb_error; |
| 768 | |
| 769 | gb_error: |
| 770 | kfree(request); |
| 771 | return retval; |
| 772 | } |
| 773 | |
| 774 | static int gb_loopback_async_ping(struct gb_loopback *gb) |
| 775 | { |
| 776 | return gb_loopback_async_operation(gb, GB_LOOPBACK_TYPE_PING, |
| 777 | NULL, 0, 0, NULL); |
| 778 | } |
| 779 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 780 | static int gb_loopback_request_handler(struct gb_operation *operation) |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 781 | { |
| 782 | struct gb_connection *connection = operation->connection; |
| 783 | struct gb_loopback_transfer_request *request; |
| 784 | struct gb_loopback_transfer_response *response; |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 785 | struct device *dev = &connection->bundle->dev; |
Bryan O'Donoghue | e51eafe | 2015-07-14 00:53:13 +0100 | [diff] [blame] | 786 | size_t len; |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 787 | |
| 788 | /* By convention, the AP initiates the version operation */ |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 789 | switch (operation->type) { |
Viresh Kumar | 0e2462d | 2015-08-14 07:57:38 +0530 | [diff] [blame] | 790 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 791 | dev_err(dev, "module-initiated version operation\n"); |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 792 | return -EINVAL; |
| 793 | case GB_LOOPBACK_TYPE_PING: |
Bryan O'Donoghue | 384a7a3 | 2015-07-13 20:20:49 +0100 | [diff] [blame] | 794 | case GB_LOOPBACK_TYPE_SINK: |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 795 | return 0; |
| 796 | case GB_LOOPBACK_TYPE_TRANSFER: |
| 797 | if (operation->request->payload_size < sizeof(*request)) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 798 | dev_err(dev, "transfer request too small (%zu < %zu)\n", |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 799 | operation->request->payload_size, |
| 800 | sizeof(*request)); |
| 801 | return -EINVAL; /* -EMSGSIZE */ |
| 802 | } |
| 803 | request = operation->request->payload; |
| 804 | len = le32_to_cpu(request->len); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 805 | if (len > gb_dev.size_max) { |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 806 | dev_err(dev, "transfer request too large (%zu > %zu)\n", |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 807 | len, gb_dev.size_max); |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 808 | return -EINVAL; |
| 809 | } |
| 810 | |
Bartosz Golaszewski | 81ad699 | 2015-11-19 13:46:43 +0100 | [diff] [blame] | 811 | if (!gb_operation_response_alloc(operation, |
| 812 | len + sizeof(*response), GFP_KERNEL)) { |
| 813 | dev_err(dev, "error allocating response\n"); |
| 814 | return -ENOMEM; |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 815 | } |
Bartosz Golaszewski | 81ad699 | 2015-11-19 13:46:43 +0100 | [diff] [blame] | 816 | response = operation->response->payload; |
| 817 | response->len = cpu_to_le32(len); |
| 818 | if (len) |
| 819 | memcpy(response->data, request->data, len); |
| 820 | |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 821 | return 0; |
| 822 | default: |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 823 | dev_err(dev, "unsupported request: %u\n", operation->type); |
Alex Elder | ac1c284 | 2015-05-11 21:16:39 -0500 | [diff] [blame] | 824 | return -EINVAL; |
| 825 | } |
| 826 | } |
| 827 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 828 | static void gb_loopback_reset_stats(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 829 | { |
| 830 | struct gb_loopback_stats reset = { |
Alex Elder | e051c82 | 2015-08-03 12:57:14 -0500 | [diff] [blame] | 831 | .min = U32_MAX, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 832 | }; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 833 | |
| 834 | /* Reset per-connection stats */ |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 835 | memcpy(&gb->latency, &reset, |
| 836 | sizeof(struct gb_loopback_stats)); |
| 837 | memcpy(&gb->throughput, &reset, |
| 838 | sizeof(struct gb_loopback_stats)); |
| 839 | memcpy(&gb->requests_per_second, &reset, |
| 840 | sizeof(struct gb_loopback_stats)); |
| 841 | memcpy(&gb->apbridge_unipro_latency, &reset, |
| 842 | sizeof(struct gb_loopback_stats)); |
| 843 | memcpy(&gb->gpbridge_firmware_latency, &reset, |
| 844 | sizeof(struct gb_loopback_stats)); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 845 | |
Bryan O'Donoghue | f42a689 | 2015-12-07 01:59:08 +0000 | [diff] [blame] | 846 | /* Should be initialized at least once per transaction set */ |
| 847 | gb->apbridge_latency_ts = 0; |
| 848 | gb->gpbridge_latency_ts = 0; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Alex Elder | a6e7e53 | 2015-08-03 12:57:13 -0500 | [diff] [blame] | 851 | 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] | 852 | { |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 853 | if (stats->min > val) |
| 854 | stats->min = val; |
| 855 | if (stats->max < val) |
| 856 | stats->max = val; |
| 857 | stats->sum += val; |
| 858 | stats->count++; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 861 | static void gb_loopback_requests_update(struct gb_loopback *gb, u32 latency) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 862 | { |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 863 | u32 req = USEC_PER_SEC; |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 864 | |
Bryan O'Donoghue | 583cbf5 | 2015-07-21 23:50:10 +0100 | [diff] [blame] | 865 | do_div(req, latency); |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 866 | gb_loopback_update_stats(&gb->requests_per_second, req); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 869 | static void gb_loopback_throughput_update(struct gb_loopback *gb, u32 latency) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 870 | { |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 871 | u32 throughput; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 872 | u32 aggregate_size = sizeof(struct gb_operation_msg_hdr) * 2; |
| 873 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 874 | switch (gb->type) { |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 875 | case GB_LOOPBACK_TYPE_PING: |
| 876 | break; |
| 877 | case GB_LOOPBACK_TYPE_SINK: |
| 878 | aggregate_size += sizeof(struct gb_loopback_transfer_request) + |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 879 | gb->size; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 880 | break; |
| 881 | case GB_LOOPBACK_TYPE_TRANSFER: |
| 882 | aggregate_size += sizeof(struct gb_loopback_transfer_request) + |
| 883 | sizeof(struct gb_loopback_transfer_response) + |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 884 | gb->size * 2; |
Bryan O'Donoghue | f7908e4 | 2015-07-13 20:20:51 +0100 | [diff] [blame] | 885 | break; |
| 886 | default: |
| 887 | return; |
| 888 | } |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 889 | |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 890 | /* Calculate bytes per second */ |
| 891 | throughput = USEC_PER_SEC; |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 892 | do_div(throughput, latency); |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 893 | throughput *= aggregate_size; |
| 894 | gb_loopback_update_stats(&gb->throughput, throughput); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 897 | static void gb_loopback_calculate_stats(struct gb_loopback *gb) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 898 | { |
| 899 | u32 lat; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 900 | |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 901 | /* Express latency in terms of microseconds */ |
Bryan O'Donoghue | bd41610 | 2015-08-17 00:55:05 +0100 | [diff] [blame] | 902 | lat = gb_loopback_nsec_to_usec_latency(gb->elapsed_nsecs); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 903 | |
Bryan O'Donoghue | 98676ca | 2015-08-17 00:55:12 +0100 | [diff] [blame] | 904 | /* Log latency stastic */ |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 905 | gb_loopback_update_stats(&gb->latency, lat); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 906 | |
| 907 | /* Raw latency log on a per thread basis */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 908 | kfifo_in(&gb->kfifo_lat, (unsigned char *)&lat, sizeof(lat)); |
Bryan O'Donoghue | 00af658 | 2015-07-21 23:50:08 +0100 | [diff] [blame] | 909 | |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 910 | /* Log throughput and requests using latency as benchmark */ |
| 911 | gb_loopback_throughput_update(gb, lat); |
| 912 | gb_loopback_requests_update(gb, lat); |
| 913 | |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 914 | /* Log the firmware supplied latency values */ |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 915 | gb_loopback_update_stats(&gb->apbridge_unipro_latency, |
| 916 | gb->apbridge_latency_ts); |
Bryan O'Donoghue | 1ec5843 | 2015-10-15 16:10:45 +0100 | [diff] [blame] | 917 | gb_loopback_update_stats(&gb->gpbridge_firmware_latency, |
| 918 | gb->gpbridge_latency_ts); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 919 | } |
| 920 | |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 921 | static void gb_loopback_async_wait_to_send(struct gb_loopback *gb) |
| 922 | { |
| 923 | if (!(gb->async && gb->outstanding_operations_max)) |
| 924 | return; |
| 925 | wait_event_interruptible(gb->wq_completion, |
| 926 | (atomic_read(&gb->outstanding_operations) < |
| 927 | gb->outstanding_operations_max) || |
| 928 | kthread_should_stop()); |
| 929 | } |
| 930 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 931 | static int gb_loopback_fn(void *data) |
| 932 | { |
| 933 | int error = 0; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 934 | int us_wait = 0; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 935 | int type; |
| 936 | u32 size; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 937 | u32 send_count = 0; |
Alex Elder | 3f12c3e | 2015-08-03 12:57:11 -0500 | [diff] [blame] | 938 | struct gb_loopback *gb = data; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 939 | |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 940 | while (1) { |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 941 | if (!gb->type) |
| 942 | wait_event_interruptible(gb->wq, gb->type || |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 943 | kthread_should_stop()); |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 944 | if (kthread_should_stop()) |
| 945 | break; |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 946 | |
Bryan O'Donoghue | 8e3fba5 | 2015-12-11 13:46:53 +0000 | [diff] [blame] | 947 | /* Limit the maximum number of in-flight async operations */ |
| 948 | gb_loopback_async_wait_to_send(gb); |
Bryan O'Donoghue | 3dfe8aa | 2015-07-24 10:02:56 +0100 | [diff] [blame] | 949 | if (kthread_should_stop()) |
| 950 | break; |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 951 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 952 | mutex_lock(&gb->mutex); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 953 | sysfs_notify(&gb->connection->bundle->dev.kobj, |
| 954 | NULL, "iteration_count"); |
| 955 | |
| 956 | /* Optionally terminate */ |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 957 | if (send_count == gb->iteration_max) { |
Greg Kroah-Hartman | 2422d36 | 2016-02-17 16:30:38 -0800 | [diff] [blame] | 958 | gb->type = 0; |
| 959 | send_count = 0; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 960 | mutex_unlock(&gb->mutex); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 961 | continue; |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 962 | } |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 963 | size = gb->size; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 964 | us_wait = gb->us_wait; |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 965 | type = gb->type; |
Bryan O'Donoghue | d9fb375 | 2015-12-03 17:29:38 +0000 | [diff] [blame] | 966 | mutex_unlock(&gb->mutex); |
| 967 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 968 | /* Else operations to perform */ |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 969 | if (gb->async) { |
| 970 | if (type == GB_LOOPBACK_TYPE_PING) { |
| 971 | error = gb_loopback_async_ping(gb); |
| 972 | gb_loopback_calculate_stats(gb); |
| 973 | } else if (type == GB_LOOPBACK_TYPE_TRANSFER) { |
| 974 | error = gb_loopback_async_transfer(gb, size); |
| 975 | } else if (type == GB_LOOPBACK_TYPE_SINK) { |
| 976 | error = gb_loopback_async_sink(gb, size); |
| 977 | } |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 978 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 979 | if (error) |
| 980 | gb->error++; |
| 981 | } else { |
| 982 | /* We are effectively single threaded here */ |
| 983 | if (type == GB_LOOPBACK_TYPE_PING) |
| 984 | error = gb_loopback_sync_ping(gb); |
| 985 | else if (type == GB_LOOPBACK_TYPE_TRANSFER) |
| 986 | error = gb_loopback_sync_transfer(gb, size); |
| 987 | else if (type == GB_LOOPBACK_TYPE_SINK) |
| 988 | error = gb_loopback_sync_sink(gb, size); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 989 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 990 | if (error) |
| 991 | gb->error++; |
| 992 | gb->iteration_count++; |
| 993 | gb_loopback_calculate_stats(gb); |
| 994 | } |
| 995 | send_count++; |
Bryan O'Donoghue | b36f04f | 2015-12-07 01:59:07 +0000 | [diff] [blame] | 996 | if (us_wait) |
| 997 | udelay(us_wait); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1002 | static int gb_loopback_dbgfs_latency_show_common(struct seq_file *s, |
| 1003 | struct kfifo *kfifo, |
| 1004 | struct mutex *mutex) |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1005 | { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1006 | u32 latency; |
| 1007 | int retval; |
| 1008 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1009 | if (kfifo_len(kfifo) == 0) { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1010 | retval = -EAGAIN; |
| 1011 | goto done; |
| 1012 | } |
| 1013 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1014 | mutex_lock(mutex); |
| 1015 | retval = kfifo_out(kfifo, &latency, sizeof(latency)); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1016 | if (retval > 0) { |
| 1017 | seq_printf(s, "%u", latency); |
| 1018 | retval = 0; |
| 1019 | } |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1020 | mutex_unlock(mutex); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1021 | done: |
| 1022 | return retval; |
| 1023 | } |
| 1024 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1025 | static int gb_loopback_dbgfs_latency_show(struct seq_file *s, void *unused) |
| 1026 | { |
| 1027 | struct gb_loopback *gb = s->private; |
| 1028 | |
| 1029 | return gb_loopback_dbgfs_latency_show_common(s, &gb->kfifo_lat, |
| 1030 | &gb->mutex); |
| 1031 | } |
| 1032 | |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1033 | static int gb_loopback_latency_open(struct inode *inode, struct file *file) |
| 1034 | { |
| 1035 | return single_open(file, gb_loopback_dbgfs_latency_show, |
| 1036 | inode->i_private); |
| 1037 | } |
| 1038 | |
| 1039 | static const struct file_operations gb_loopback_debugfs_latency_ops = { |
| 1040 | .open = gb_loopback_latency_open, |
| 1041 | .read = seq_read, |
| 1042 | .llseek = seq_lseek, |
| 1043 | .release = single_release, |
| 1044 | }; |
| 1045 | |
Bryan O'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1046 | static int gb_loopback_bus_id_compare(void *priv, struct list_head *lha, |
| 1047 | struct list_head *lhb) |
| 1048 | { |
| 1049 | struct gb_loopback *a = list_entry(lha, struct gb_loopback, entry); |
| 1050 | struct gb_loopback *b = list_entry(lhb, struct gb_loopback, entry); |
| 1051 | struct gb_connection *ca = a->connection; |
| 1052 | struct gb_connection *cb = b->connection; |
| 1053 | |
Bryan O'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1054 | if (ca->bundle->intf->interface_id < cb->bundle->intf->interface_id) |
| 1055 | return -1; |
| 1056 | if (cb->bundle->intf->interface_id < ca->bundle->intf->interface_id) |
| 1057 | return 1; |
| 1058 | if (ca->bundle->id < cb->bundle->id) |
| 1059 | return -1; |
| 1060 | if (cb->bundle->id < ca->bundle->id) |
| 1061 | return 1; |
| 1062 | if (ca->intf_cport_id < cb->intf_cport_id) |
| 1063 | return -1; |
| 1064 | else if (cb->intf_cport_id < ca->intf_cport_id) |
| 1065 | return 1; |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | static void gb_loopback_insert_id(struct gb_loopback *gb) |
| 1071 | { |
| 1072 | struct gb_loopback *gb_list; |
| 1073 | u32 new_lbid = 0; |
| 1074 | |
| 1075 | /* perform an insertion sort */ |
| 1076 | list_add_tail(&gb->entry, &gb_dev.list); |
| 1077 | list_sort(NULL, &gb_dev.list, gb_loopback_bus_id_compare); |
| 1078 | list_for_each_entry(gb_list, &gb_dev.list, entry) { |
| 1079 | gb_list->lbid = 1 << new_lbid; |
| 1080 | new_lbid++; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1084 | #define DEBUGFS_NAMELEN 32 |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1085 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1086 | static int gb_loopback_probe(struct gb_bundle *bundle, |
| 1087 | const struct greybus_bundle_id *id) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1088 | { |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1089 | struct greybus_descriptor_cport *cport_desc; |
| 1090 | struct gb_connection *connection; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1091 | struct gb_loopback *gb; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1092 | struct device *dev; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1093 | int retval; |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1094 | char name[DEBUGFS_NAMELEN]; |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1095 | unsigned long flags; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1096 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1097 | if (bundle->num_cports != 1) |
| 1098 | return -ENODEV; |
| 1099 | |
| 1100 | cport_desc = &bundle->cport_desc[0]; |
| 1101 | if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LOOPBACK) |
| 1102 | return -ENODEV; |
| 1103 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1104 | gb = kzalloc(sizeof(*gb), GFP_KERNEL); |
| 1105 | if (!gb) |
| 1106 | return -ENOMEM; |
| 1107 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1108 | connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id), |
| 1109 | gb_loopback_request_handler); |
| 1110 | if (IS_ERR(connection)) { |
| 1111 | retval = PTR_ERR(connection); |
| 1112 | goto out_kzalloc; |
| 1113 | } |
| 1114 | |
| 1115 | gb->connection = connection; |
| 1116 | greybus_set_drvdata(bundle, gb); |
| 1117 | |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1118 | init_waitqueue_head(&gb->wq); |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 1119 | init_waitqueue_head(&gb->wq_completion); |
| 1120 | atomic_set(&gb->outstanding_operations, 0); |
Bryan O'Donoghue | 8e1d6c3 | 2015-12-03 17:29:41 +0000 | [diff] [blame] | 1121 | gb_loopback_reset_stats(gb); |
| 1122 | |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1123 | /* Reported values to user-space for min/max timeouts */ |
| 1124 | gb->timeout_min = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MIN); |
| 1125 | gb->timeout_max = jiffies_to_usecs(GB_LOOPBACK_TIMEOUT_MAX); |
| 1126 | |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1127 | if (!gb_dev.count) { |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1128 | /* Calculate maximum payload */ |
| 1129 | gb_dev.size_max = gb_operation_get_payload_size_max(connection); |
| 1130 | if (gb_dev.size_max <= |
| 1131 | sizeof(struct gb_loopback_transfer_request)) { |
| 1132 | retval = -EINVAL; |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1133 | goto out_connection_destroy; |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1134 | } |
| 1135 | gb_dev.size_max -= sizeof(struct gb_loopback_transfer_request); |
| 1136 | } |
Bryan O'Donoghue | f06272b | 2015-08-17 00:55:11 +0100 | [diff] [blame] | 1137 | |
| 1138 | /* Create per-connection sysfs and debugfs data-points */ |
Bryan O'Donoghue | 8d8d36d | 2015-09-23 09:31:33 -0700 | [diff] [blame] | 1139 | snprintf(name, sizeof(name), "raw_latency_%s", |
Greg Kroah-Hartman | d9a9ea1 | 2015-10-16 16:55:46 -0700 | [diff] [blame] | 1140 | dev_name(&connection->bundle->dev)); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1141 | gb->file = debugfs_create_file(name, S_IFREG | S_IRUGO, gb_dev.root, gb, |
| 1142 | &gb_loopback_debugfs_latency_ops); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1143 | |
| 1144 | gb->id = ida_simple_get(&loopback_ida, 0, 0, GFP_KERNEL); |
| 1145 | if (gb->id < 0) { |
| 1146 | retval = gb->id; |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1147 | goto out_debugfs_remove; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1150 | retval = gb_connection_enable(connection); |
| 1151 | if (retval) |
| 1152 | goto out_ida_remove; |
| 1153 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1154 | dev = device_create_with_groups(&loopback_class, |
| 1155 | &connection->bundle->dev, |
| 1156 | MKDEV(0, 0), gb, loopback_groups, |
| 1157 | "gb_loopback%d", gb->id); |
| 1158 | if (IS_ERR(dev)) { |
| 1159 | retval = PTR_ERR(dev); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1160 | goto out_connection_disable; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1161 | } |
| 1162 | gb->dev = dev; |
Bryan O'Donoghue | c3bba87 | 2015-07-13 20:20:50 +0100 | [diff] [blame] | 1163 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1164 | /* Allocate kfifo */ |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1165 | if (kfifo_alloc(&gb->kfifo_lat, kfifo_depth * sizeof(u32), |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1166 | GFP_KERNEL)) { |
| 1167 | retval = -ENOMEM; |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1168 | goto out_conn; |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1169 | } |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1170 | if (kfifo_alloc(&gb->kfifo_ts, kfifo_depth * sizeof(struct timeval) * 2, |
| 1171 | GFP_KERNEL)) { |
| 1172 | retval = -ENOMEM; |
| 1173 | goto out_kfifo0; |
| 1174 | } |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* Fork worker thread */ |
Bryan O'Donoghue | 85d678c | 2015-07-28 18:34:36 +0100 | [diff] [blame] | 1177 | mutex_init(&gb->mutex); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1178 | gb->task = kthread_run(gb_loopback_fn, gb, "gb_loopback"); |
| 1179 | if (IS_ERR(gb->task)) { |
Alex Elder | 69f6034 | 2015-05-11 21:16:35 -0500 | [diff] [blame] | 1180 | retval = PTR_ERR(gb->task); |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1181 | goto out_kfifo1; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1184 | spin_lock_irqsave(&gb_dev.lock, flags); |
Bryan O'Donoghue | 5015115 | 2015-09-14 10:48:46 +0100 | [diff] [blame] | 1185 | gb_loopback_insert_id(gb); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1186 | gb_dev.count++; |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1187 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 1188 | |
| 1189 | gb_connection_latency_tag_enable(connection); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1190 | return 0; |
| 1191 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1192 | out_kfifo1: |
| 1193 | kfifo_free(&gb->kfifo_ts); |
| 1194 | out_kfifo0: |
| 1195 | kfifo_free(&gb->kfifo_lat); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1196 | out_conn: |
| 1197 | device_unregister(dev); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1198 | out_connection_disable: |
| 1199 | gb_connection_disable(connection); |
| 1200 | out_ida_remove: |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1201 | ida_simple_remove(&loopback_ida, gb->id); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1202 | out_debugfs_remove: |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1203 | debugfs_remove(gb->file); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1204 | out_connection_destroy: |
| 1205 | gb_connection_destroy(connection); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1206 | out_kzalloc: |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1207 | kfree(gb); |
Alex Elder | 1fb807c | 2015-08-03 12:57:20 -0500 | [diff] [blame] | 1208 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1209 | return retval; |
| 1210 | } |
| 1211 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1212 | static void gb_loopback_disconnect(struct gb_bundle *bundle) |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1213 | { |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1214 | struct gb_loopback *gb = greybus_get_drvdata(bundle); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1215 | unsigned long flags; |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1216 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1217 | gb_connection_disable(gb->connection); |
| 1218 | |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1219 | if (!IS_ERR_OR_NULL(gb->task)) |
| 1220 | kthread_stop(gb->task); |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1221 | |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1222 | kfifo_free(&gb->kfifo_lat); |
| 1223 | kfifo_free(&gb->kfifo_ts); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1224 | gb_connection_latency_tag_disable(gb->connection); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1225 | debugfs_remove(gb->file); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1226 | |
| 1227 | /* |
| 1228 | * FIXME: gb_loopback_async_wait_all() is redundant now, as connection |
| 1229 | * is disabled at the beginning and so we can't have any more |
| 1230 | * incoming/outgoing requests. |
| 1231 | */ |
Bryan O'Donoghue | 36f241ff | 2015-12-11 13:46:52 +0000 | [diff] [blame] | 1232 | gb_loopback_async_wait_all(gb); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1233 | |
| 1234 | spin_lock_irqsave(&gb_dev.lock, flags); |
| 1235 | gb_dev.count--; |
Bryan O'Donoghue | ff477d0 | 2015-09-04 16:53:31 +0100 | [diff] [blame] | 1236 | list_del(&gb->entry); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1237 | spin_unlock_irqrestore(&gb_dev.lock, flags); |
| 1238 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1239 | device_unregister(gb->dev); |
| 1240 | ida_simple_remove(&loopback_ida, gb->id); |
| 1241 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1242 | gb_connection_destroy(gb->connection); |
Bryan O'Donoghue | 5f3e0d1 | 2015-09-14 10:48:41 +0100 | [diff] [blame] | 1243 | kfree(gb); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1246 | static const struct greybus_bundle_id gb_loopback_id_table[] = { |
| 1247 | { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LOOPBACK) }, |
| 1248 | { } |
| 1249 | }; |
| 1250 | MODULE_DEVICE_TABLE(greybus, gb_loopback_id_table); |
| 1251 | |
| 1252 | static struct greybus_driver gb_loopback_driver = { |
| 1253 | .name = "loopback", |
| 1254 | .probe = gb_loopback_probe, |
| 1255 | .disconnect = gb_loopback_disconnect, |
| 1256 | .id_table = gb_loopback_id_table, |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1257 | }; |
| 1258 | |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1259 | static int loopback_init(void) |
| 1260 | { |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1261 | int retval; |
| 1262 | |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1263 | INIT_LIST_HEAD(&gb_dev.list); |
Bryan O'Donoghue | 1292783 | 2015-12-07 01:59:06 +0000 | [diff] [blame] | 1264 | INIT_LIST_HEAD(&gb_dev.list_op_async); |
Bryan O'Donoghue | 2e238d71 | 2015-12-07 01:59:05 +0000 | [diff] [blame] | 1265 | spin_lock_init(&gb_dev.lock); |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1266 | gb_dev.root = debugfs_create_dir("gb_loopback", NULL); |
Bryan O'Donoghue | 67d1eec | 2015-08-17 00:55:04 +0100 | [diff] [blame] | 1267 | |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1268 | retval = class_register(&loopback_class); |
| 1269 | if (retval) |
| 1270 | goto err; |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1271 | |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1272 | retval = greybus_register(&gb_loopback_driver); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1273 | if (retval) |
| 1274 | goto err_unregister; |
| 1275 | |
| 1276 | return 0; |
| 1277 | |
| 1278 | err_unregister: |
| 1279 | class_unregister(&loopback_class); |
| 1280 | err: |
Bryan O'Donoghue | 4b0ea00c | 2015-08-17 00:55:07 +0100 | [diff] [blame] | 1281 | debugfs_remove_recursive(gb_dev.root); |
| 1282 | return retval; |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1283 | } |
| 1284 | module_init(loopback_init); |
| 1285 | |
| 1286 | static void __exit loopback_exit(void) |
| 1287 | { |
Bryan O'Donoghue | aa27bf8 | 2015-08-17 00:55:03 +0100 | [diff] [blame] | 1288 | debugfs_remove_recursive(gb_dev.root); |
Viresh Kumar | e82a11d | 2016-02-12 16:08:29 +0530 | [diff] [blame] | 1289 | greybus_deregister(&gb_loopback_driver); |
Axel Haslam | 079fa32 | 2015-12-11 11:49:45 +0100 | [diff] [blame] | 1290 | class_unregister(&loopback_class); |
| 1291 | ida_destroy(&loopback_ida); |
Bryan O'Donoghue | cbd204b | 2015-07-28 18:34:38 +0100 | [diff] [blame] | 1292 | } |
| 1293 | module_exit(loopback_exit); |
Alexandre Bailon | 355a705 | 2015-03-31 09:51:59 +0200 | [diff] [blame] | 1294 | |
| 1295 | MODULE_LICENSE("GPL v2"); |