blob: e4412c7d25b0e057303927a3c81c56ce4269a35d [file] [log] [blame]
Greg Kroah-Hartman1a79f222017-11-07 14:58:50 +01001// SPDX-License-Identifier: GPL-2.0
Christian Gromm57562a72015-07-24 16:11:48 +02002/*
3 * core.c - Implementation of core module of MOST Linux driver stack
4 *
Christian Grommca78e042020-01-14 16:57:50 +01005 * Copyright (C) 2013-2020 Microchip Technology Germany II GmbH & Co. KG
Christian Gromm57562a72015-07-24 16:11:48 +02006 */
7
Christian Gromm57562a72015-07-24 16:11:48 +02008#include <linux/module.h>
9#include <linux/fs.h>
10#include <linux/slab.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/list.h>
14#include <linux/poll.h>
15#include <linux/wait.h>
16#include <linux/kobject.h>
17#include <linux/mutex.h>
18#include <linux/completion.h>
19#include <linux/sysfs.h>
20#include <linux/kthread.h>
21#include <linux/dma-mapping.h>
22#include <linux/idr.h>
Christian Grommb2765272020-03-10 14:02:40 +010023#include <linux/most.h>
Christian Gromm57562a72015-07-24 16:11:48 +020024
25#define MAX_CHANNELS 64
26#define STRING_SIZE 80
27
Christian Gromm57562a72015-07-24 16:11:48 +020028static struct ida mdev_id;
Christian Gromm71457d42015-09-28 17:18:45 +020029static int dummy_num_buffers;
Christian Grommd693e902020-01-23 16:38:20 +010030static struct list_head comp_list;
Christian Gromm14ae5f02017-11-21 15:04:51 +010031
Christian Gromm7faeffe2017-11-21 15:04:48 +010032struct pipe {
Christian Gromm45917e72019-12-13 13:04:15 +010033 struct most_component *comp;
Christian Grommccfbaee2015-09-28 17:18:46 +020034 int refs;
35 int num_buffers;
36};
37
Christian Grommfcb7fad2017-11-21 15:04:47 +010038struct most_channel {
Christian Gromm4d5f0222017-11-21 15:04:43 +010039 struct device dev;
Christian Gromm57562a72015-07-24 16:11:48 +020040 struct completion cleanup;
41 atomic_t mbo_ref;
42 atomic_t mbo_nq_level;
Christian Gromm2aa9b962015-10-21 17:50:49 +020043 u16 channel_id;
Christian Gromm845101b2017-11-21 15:04:57 +010044 char name[STRING_SIZE];
Christian Gromm57562a72015-07-24 16:11:48 +020045 bool is_poisoned;
Christian Grommaf96ce02019-11-12 14:40:36 +010046 struct mutex start_mutex; /* channel activation synchronization */
Christian Grommbf9503f2016-08-19 11:12:54 +020047 struct mutex nq_mutex; /* nq thread synchronization */
Christian Gromm57562a72015-07-24 16:11:48 +020048 int is_starving;
49 struct most_interface *iface;
Christian Gromm57562a72015-07-24 16:11:48 +020050 struct most_channel_config cfg;
51 bool keep_mbo;
52 bool enqueue_halt;
53 struct list_head fifo;
Christian Grommaf96ce02019-11-12 14:40:36 +010054 spinlock_t fifo_lock; /* fifo access synchronization */
Christian Gromm57562a72015-07-24 16:11:48 +020055 struct list_head halt_fifo;
56 struct list_head list;
Christian Grommf898f982017-11-21 15:04:50 +010057 struct pipe pipe0;
58 struct pipe pipe1;
Christian Gromm57562a72015-07-24 16:11:48 +020059 struct list_head trash_fifo;
60 struct task_struct *hdm_enqueue_task;
Christian Gromm57562a72015-07-24 16:11:48 +020061 wait_queue_head_t hdm_fifo_wq;
Christian Grommed021a02017-11-21 15:05:01 +010062
Christian Gromm57562a72015-07-24 16:11:48 +020063};
Christian Gromm9cbe5aa2015-10-21 17:50:45 +020064
Christian Grommfcb7fad2017-11-21 15:04:47 +010065#define to_channel(d) container_of(d, struct most_channel, dev)
Christian Gromm57562a72015-07-24 16:11:48 +020066
Christian Gromm9136fcc2017-11-21 15:04:56 +010067struct interface_private {
Christian Gromm57562a72015-07-24 16:11:48 +020068 int dev_id;
Christian Gromm9136fcc2017-11-21 15:04:56 +010069 char name[STRING_SIZE];
Christian Grommfcb7fad2017-11-21 15:04:47 +010070 struct most_channel *channel[MAX_CHANNELS];
Christian Gromm9136fcc2017-11-21 15:04:56 +010071 struct list_head channel_list;
Christian Gromm57562a72015-07-24 16:11:48 +020072};
Christian Gromm9cbe5aa2015-10-21 17:50:45 +020073
Hari Prasath Gujulan Elangoe7f2b702015-12-28 08:55:37 +000074static const struct {
75 int most_ch_data_type;
Kees Cook06324662017-05-08 15:59:05 -070076 const char *name;
Christian Gromm95f73012016-09-21 14:49:09 +020077} ch_data_type[] = {
Christian Gromm4df09912019-11-07 15:49:28 +010078 { MOST_CH_CONTROL, "control" },
79 { MOST_CH_ASYNC, "async" },
80 { MOST_CH_SYNC, "sync" },
81 { MOST_CH_ISOC, "isoc"},
82 { MOST_CH_ISOC, "isoc_avp"},
Christian Gromm95f73012016-09-21 14:49:09 +020083};
Hari Prasath Gujulan Elangoe7f2b702015-12-28 08:55:37 +000084
Christian Gromm57562a72015-07-24 16:11:48 +020085/**
86 * list_pop_mbo - retrieves the first MBO of the list and removes it
87 * @ptr: the list head to grab the MBO from.
88 */
89#define list_pop_mbo(ptr) \
90({ \
91 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
92 list_del(&_mbo->list); \
93 _mbo; \
94})
95
Christian Gromm57562a72015-07-24 16:11:48 +020096/**
Christian Gromm57562a72015-07-24 16:11:48 +020097 * most_free_mbo_coherent - free an MBO and its coherent buffer
Christian Grommb7937dc2017-11-21 15:05:12 +010098 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +020099 */
100static void most_free_mbo_coherent(struct mbo *mbo)
101{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100102 struct most_channel *c = mbo->context;
Christian Gromm57562a72015-07-24 16:11:48 +0200103 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
104
Christian Gromm3598cec2018-05-08 11:45:03 +0200105 if (c->iface->dma_free)
106 c->iface->dma_free(mbo, coherent_buf_size);
107 else
108 kfree(mbo->virt_address);
Christian Gromm57562a72015-07-24 16:11:48 +0200109 kfree(mbo);
110 if (atomic_sub_and_test(1, &c->mbo_ref))
111 complete(&c->cleanup);
112}
113
114/**
115 * flush_channel_fifos - clear the channel fifos
116 * @c: pointer to channel object
117 */
Christian Grommfcb7fad2017-11-21 15:04:47 +0100118static void flush_channel_fifos(struct most_channel *c)
Christian Gromm57562a72015-07-24 16:11:48 +0200119{
120 unsigned long flags, hf_flags;
121 struct mbo *mbo, *tmp;
122
123 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
124 return;
125
126 spin_lock_irqsave(&c->fifo_lock, flags);
127 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
128 list_del(&mbo->list);
129 spin_unlock_irqrestore(&c->fifo_lock, flags);
Sudip Mukherjee0834be62015-09-04 16:22:05 +0530130 most_free_mbo_coherent(mbo);
Christian Gromm57562a72015-07-24 16:11:48 +0200131 spin_lock_irqsave(&c->fifo_lock, flags);
132 }
133 spin_unlock_irqrestore(&c->fifo_lock, flags);
134
135 spin_lock_irqsave(&c->fifo_lock, hf_flags);
136 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
137 list_del(&mbo->list);
138 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
Sudip Mukherjee0834be62015-09-04 16:22:05 +0530139 most_free_mbo_coherent(mbo);
Christian Gromm57562a72015-07-24 16:11:48 +0200140 spin_lock_irqsave(&c->fifo_lock, hf_flags);
141 }
142 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
143
144 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
Christian Grommb7935e52020-01-23 16:38:21 +0100145 dev_warn(&c->dev, "Channel or trash fifo not empty\n");
Christian Gromm57562a72015-07-24 16:11:48 +0200146}
147
148/**
149 * flush_trash_fifo - clear the trash fifo
150 * @c: pointer to channel object
151 */
Christian Grommfcb7fad2017-11-21 15:04:47 +0100152static int flush_trash_fifo(struct most_channel *c)
Christian Gromm57562a72015-07-24 16:11:48 +0200153{
154 struct mbo *mbo, *tmp;
155 unsigned long flags;
156
157 spin_lock_irqsave(&c->fifo_lock, flags);
158 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
159 list_del(&mbo->list);
160 spin_unlock_irqrestore(&c->fifo_lock, flags);
161 most_free_mbo_coherent(mbo);
162 spin_lock_irqsave(&c->fifo_lock, flags);
163 }
164 spin_unlock_irqrestore(&c->fifo_lock, flags);
165 return 0;
166}
167
Christian Gromm4d5f0222017-11-21 15:04:43 +0100168static ssize_t available_directions_show(struct device *dev,
169 struct device_attribute *attr,
Christian Grommedaa1e32015-10-21 17:50:43 +0200170 char *buf)
Christian Gromm57562a72015-07-24 16:11:48 +0200171{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100172 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200173 unsigned int i = c->channel_id;
174
175 strcpy(buf, "");
176 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
Christian Gromm95f73012016-09-21 14:49:09 +0200177 strcat(buf, "rx ");
Christian Gromm57562a72015-07-24 16:11:48 +0200178 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
Christian Gromm95f73012016-09-21 14:49:09 +0200179 strcat(buf, "tx ");
Christian Gromm57562a72015-07-24 16:11:48 +0200180 strcat(buf, "\n");
Andrey Shvetsov22ff1952016-09-21 14:49:08 +0200181 return strlen(buf);
Christian Gromm57562a72015-07-24 16:11:48 +0200182}
183
Christian Gromm4d5f0222017-11-21 15:04:43 +0100184static ssize_t available_datatypes_show(struct device *dev,
185 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200186 char *buf)
187{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100188 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200189 unsigned int i = c->channel_id;
190
191 strcpy(buf, "");
192 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
193 strcat(buf, "control ");
194 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
195 strcat(buf, "async ");
196 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
197 strcat(buf, "sync ");
Andrey Shvetsov05406092016-09-21 14:49:10 +0200198 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
Christian Gromm95f73012016-09-21 14:49:09 +0200199 strcat(buf, "isoc ");
Christian Gromm57562a72015-07-24 16:11:48 +0200200 strcat(buf, "\n");
Andrey Shvetsov22ff1952016-09-21 14:49:08 +0200201 return strlen(buf);
Christian Gromm57562a72015-07-24 16:11:48 +0200202}
203
Christian Gromm4d5f0222017-11-21 15:04:43 +0100204static ssize_t number_of_packet_buffers_show(struct device *dev,
205 struct device_attribute *attr,
Christian Gromm4dd7c7c2017-04-07 15:38:32 +0200206 char *buf)
Christian Gromm57562a72015-07-24 16:11:48 +0200207{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100208 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200209 unsigned int i = c->channel_id;
210
211 return snprintf(buf, PAGE_SIZE, "%d\n",
212 c->iface->channel_vector[i].num_buffers_packet);
213}
214
Christian Gromm4d5f0222017-11-21 15:04:43 +0100215static ssize_t number_of_stream_buffers_show(struct device *dev,
216 struct device_attribute *attr,
Christian Gromm4dd7c7c2017-04-07 15:38:32 +0200217 char *buf)
Christian Gromm57562a72015-07-24 16:11:48 +0200218{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100219 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200220 unsigned int i = c->channel_id;
221
222 return snprintf(buf, PAGE_SIZE, "%d\n",
223 c->iface->channel_vector[i].num_buffers_streaming);
224}
225
Christian Gromm4d5f0222017-11-21 15:04:43 +0100226static ssize_t size_of_packet_buffer_show(struct device *dev,
227 struct device_attribute *attr,
Christian Gromm4dd7c7c2017-04-07 15:38:32 +0200228 char *buf)
Christian Gromm57562a72015-07-24 16:11:48 +0200229{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100230 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200231 unsigned int i = c->channel_id;
232
233 return snprintf(buf, PAGE_SIZE, "%d\n",
234 c->iface->channel_vector[i].buffer_size_packet);
235}
236
Christian Gromm4d5f0222017-11-21 15:04:43 +0100237static ssize_t size_of_stream_buffer_show(struct device *dev,
238 struct device_attribute *attr,
Christian Gromm4dd7c7c2017-04-07 15:38:32 +0200239 char *buf)
Christian Gromm57562a72015-07-24 16:11:48 +0200240{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100241 struct most_channel *c = to_channel(dev);
Christian Gromm57562a72015-07-24 16:11:48 +0200242 unsigned int i = c->channel_id;
243
244 return snprintf(buf, PAGE_SIZE, "%d\n",
245 c->iface->channel_vector[i].buffer_size_streaming);
246}
247
Christian Gromm4d5f0222017-11-21 15:04:43 +0100248static ssize_t channel_starving_show(struct device *dev,
249 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200250 char *buf)
251{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100252 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100253
Christian Gromm57562a72015-07-24 16:11:48 +0200254 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
255}
256
Christian Gromm4d5f0222017-11-21 15:04:43 +0100257static ssize_t set_number_of_buffers_show(struct device *dev,
258 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200259 char *buf)
260{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100261 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100262
Christian Gromm57562a72015-07-24 16:11:48 +0200263 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
264}
265
Christian Gromm4d5f0222017-11-21 15:04:43 +0100266static ssize_t set_buffer_size_show(struct device *dev,
267 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200268 char *buf)
269{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100270 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100271
Christian Gromm57562a72015-07-24 16:11:48 +0200272 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
273}
274
Christian Gromm4d5f0222017-11-21 15:04:43 +0100275static ssize_t set_direction_show(struct device *dev,
276 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200277 char *buf)
278{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100279 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100280
Christian Gromm57562a72015-07-24 16:11:48 +0200281 if (c->cfg.direction & MOST_CH_TX)
Christian Gromm95f73012016-09-21 14:49:09 +0200282 return snprintf(buf, PAGE_SIZE, "tx\n");
Christian Gromm57562a72015-07-24 16:11:48 +0200283 else if (c->cfg.direction & MOST_CH_RX)
Christian Gromm95f73012016-09-21 14:49:09 +0200284 return snprintf(buf, PAGE_SIZE, "rx\n");
Christian Gromm57562a72015-07-24 16:11:48 +0200285 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
286}
287
Christian Gromm4d5f0222017-11-21 15:04:43 +0100288static ssize_t set_datatype_show(struct device *dev,
289 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200290 char *buf)
291{
Hari Prasath Gujulan Elangoe7f2b702015-12-28 08:55:37 +0000292 int i;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100293 struct most_channel *c = to_channel(dev);
Hari Prasath Gujulan Elangoe7f2b702015-12-28 08:55:37 +0000294
295 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
296 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
Peikan Tsaif419f882019-08-26 01:58:49 +0800297 return snprintf(buf, PAGE_SIZE, "%s",
298 ch_data_type[i].name);
Hari Prasath Gujulan Elangoe7f2b702015-12-28 08:55:37 +0000299 }
Christian Gromm57562a72015-07-24 16:11:48 +0200300 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
301}
302
Christian Gromm4d5f0222017-11-21 15:04:43 +0100303static ssize_t set_subbuffer_size_show(struct device *dev,
304 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200305 char *buf)
306{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100307 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100308
Christian Gromm57562a72015-07-24 16:11:48 +0200309 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
310}
311
Christian Gromm4d5f0222017-11-21 15:04:43 +0100312static ssize_t set_packets_per_xact_show(struct device *dev,
313 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200314 char *buf)
315{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100316 struct most_channel *c = to_channel(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100317
Christian Gromm57562a72015-07-24 16:11:48 +0200318 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
319}
320
Christian Grommdbd36d52018-05-08 11:44:53 +0200321static ssize_t set_dbr_size_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
323{
324 struct most_channel *c = to_channel(dev);
325
326 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
327}
328
Christian Gromm4ad86622018-08-13 15:02:29 +0200329#define to_dev_attr(a) container_of(a, struct device_attribute, attr)
330static umode_t channel_attr_is_visible(struct kobject *kobj,
331 struct attribute *attr, int index)
332{
333 struct device_attribute *dev_attr = to_dev_attr(attr);
334 struct device *dev = kobj_to_dev(kobj);
335 struct most_channel *c = to_channel(dev);
336
337 if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
338 (c->iface->interface != ITYPE_MEDIALB_DIM2))
339 return 0;
340 if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
341 (c->iface->interface != ITYPE_USB))
342 return 0;
343
344 return attr->mode;
345}
346
Christian Gromm4d5f0222017-11-21 15:04:43 +0100347#define DEV_ATTR(_name) (&dev_attr_##_name.attr)
Christian Gromm57562a72015-07-24 16:11:48 +0200348
Christian Gromm4d5f0222017-11-21 15:04:43 +0100349static DEVICE_ATTR_RO(available_directions);
350static DEVICE_ATTR_RO(available_datatypes);
351static DEVICE_ATTR_RO(number_of_packet_buffers);
352static DEVICE_ATTR_RO(number_of_stream_buffers);
353static DEVICE_ATTR_RO(size_of_stream_buffer);
354static DEVICE_ATTR_RO(size_of_packet_buffer);
355static DEVICE_ATTR_RO(channel_starving);
Christian Gromm787105b2019-04-03 15:19:49 +0200356static DEVICE_ATTR_RO(set_buffer_size);
357static DEVICE_ATTR_RO(set_number_of_buffers);
358static DEVICE_ATTR_RO(set_direction);
359static DEVICE_ATTR_RO(set_datatype);
360static DEVICE_ATTR_RO(set_subbuffer_size);
361static DEVICE_ATTR_RO(set_packets_per_xact);
362static DEVICE_ATTR_RO(set_dbr_size);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100363
364static struct attribute *channel_attrs[] = {
365 DEV_ATTR(available_directions),
366 DEV_ATTR(available_datatypes),
367 DEV_ATTR(number_of_packet_buffers),
368 DEV_ATTR(number_of_stream_buffers),
369 DEV_ATTR(size_of_stream_buffer),
370 DEV_ATTR(size_of_packet_buffer),
371 DEV_ATTR(channel_starving),
372 DEV_ATTR(set_buffer_size),
373 DEV_ATTR(set_number_of_buffers),
374 DEV_ATTR(set_direction),
375 DEV_ATTR(set_datatype),
376 DEV_ATTR(set_subbuffer_size),
377 DEV_ATTR(set_packets_per_xact),
Christian Grommdbd36d52018-05-08 11:44:53 +0200378 DEV_ATTR(set_dbr_size),
Christian Gromm57562a72015-07-24 16:11:48 +0200379 NULL,
380};
381
Rikard Falkeborn26c2e922021-01-08 23:15:12 +0100382static const struct attribute_group channel_attr_group = {
Christian Gromm4d5f0222017-11-21 15:04:43 +0100383 .attrs = channel_attrs,
Christian Gromm4ad86622018-08-13 15:02:29 +0200384 .is_visible = channel_attr_is_visible,
Christian Gromm57562a72015-07-24 16:11:48 +0200385};
386
Christian Gromm4d5f0222017-11-21 15:04:43 +0100387static const struct attribute_group *channel_attr_groups[] = {
388 &channel_attr_group,
389 NULL,
390};
Christian Gromm57562a72015-07-24 16:11:48 +0200391
Christian Gromm4d5f0222017-11-21 15:04:43 +0100392static ssize_t description_show(struct device *dev,
393 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200394 char *buf)
395{
Christian Gromm723de0f2020-01-23 16:38:17 +0100396 struct most_interface *iface = dev_get_drvdata(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100397
398 return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
Christian Gromm57562a72015-07-24 16:11:48 +0200399}
400
Christian Gromm4d5f0222017-11-21 15:04:43 +0100401static ssize_t interface_show(struct device *dev,
402 struct device_attribute *attr,
Christian Gromm57562a72015-07-24 16:11:48 +0200403 char *buf)
404{
Christian Gromm723de0f2020-01-23 16:38:17 +0100405 struct most_interface *iface = dev_get_drvdata(dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +0100406
407 switch (iface->interface) {
Christian Gromm57562a72015-07-24 16:11:48 +0200408 case ITYPE_LOOPBACK:
409 return snprintf(buf, PAGE_SIZE, "loopback\n");
410 case ITYPE_I2C:
411 return snprintf(buf, PAGE_SIZE, "i2c\n");
412 case ITYPE_I2S:
413 return snprintf(buf, PAGE_SIZE, "i2s\n");
414 case ITYPE_TSI:
415 return snprintf(buf, PAGE_SIZE, "tsi\n");
416 case ITYPE_HBI:
417 return snprintf(buf, PAGE_SIZE, "hbi\n");
418 case ITYPE_MEDIALB_DIM:
419 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
420 case ITYPE_MEDIALB_DIM2:
421 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
422 case ITYPE_USB:
423 return snprintf(buf, PAGE_SIZE, "usb\n");
424 case ITYPE_PCIE:
425 return snprintf(buf, PAGE_SIZE, "pcie\n");
426 }
427 return snprintf(buf, PAGE_SIZE, "unknown\n");
428}
429
Christian Gromm4d5f0222017-11-21 15:04:43 +0100430static DEVICE_ATTR_RO(description);
431static DEVICE_ATTR_RO(interface);
Christian Gromm57562a72015-07-24 16:11:48 +0200432
Christian Gromm4d5f0222017-11-21 15:04:43 +0100433static struct attribute *interface_attrs[] = {
434 DEV_ATTR(description),
435 DEV_ATTR(interface),
Christian Gromm57562a72015-07-24 16:11:48 +0200436 NULL,
437};
438
Rikard Falkeborn26c2e922021-01-08 23:15:12 +0100439static const struct attribute_group interface_attr_group = {
Christian Gromm4d5f0222017-11-21 15:04:43 +0100440 .attrs = interface_attrs,
Christian Gromm57562a72015-07-24 16:11:48 +0200441};
442
Christian Gromm4d5f0222017-11-21 15:04:43 +0100443static const struct attribute_group *interface_attr_groups[] = {
444 &interface_attr_group,
445 NULL,
446};
Christian Gromm57562a72015-07-24 16:11:48 +0200447
Christian Gromm45917e72019-12-13 13:04:15 +0100448static struct most_component *match_component(char *name)
Christian Grommbdafb7e2017-11-21 15:04:53 +0100449{
Christian Gromm45917e72019-12-13 13:04:15 +0100450 struct most_component *comp;
Christian Grommbdafb7e2017-11-21 15:04:53 +0100451
Christian Grommd693e902020-01-23 16:38:20 +0100452 list_for_each_entry(comp, &comp_list, list) {
Christian Gromm5a5abf02017-11-21 15:05:03 +0100453 if (!strcmp(comp->name, name))
454 return comp;
Christian Grommbdafb7e2017-11-21 15:04:53 +0100455 }
456 return NULL;
457}
458
Andrey Shvetsove7e3ce02017-11-21 15:05:22 +0100459struct show_links_data {
460 int offs;
461 char *buf;
462};
463
Colin Ian King845c31d2017-12-10 23:20:58 +0000464static int print_links(struct device *dev, void *data)
Christian Gromm57562a72015-07-24 16:11:48 +0200465{
Andrey Shvetsove7e3ce02017-11-21 15:05:22 +0100466 struct show_links_data *d = data;
467 int offs = d->offs;
468 char *buf = d->buf;
Christian Gromm9136fcc2017-11-21 15:04:56 +0100469 struct most_channel *c;
Christian Gromm723de0f2020-01-23 16:38:17 +0100470 struct most_interface *iface = dev_get_drvdata(dev);
Christian Grommbc5f96a2016-09-12 16:26:13 +0200471
Christian Gromm9136fcc2017-11-21 15:04:56 +0100472 list_for_each_entry(c, &iface->p->channel_list, list) {
Christian Gromm5a5abf02017-11-21 15:05:03 +0100473 if (c->pipe0.comp) {
Takashi Iwai234ff542020-03-11 10:19:44 +0100474 offs += scnprintf(buf + offs,
Christian Gromm9136fcc2017-11-21 15:04:56 +0100475 PAGE_SIZE - offs,
476 "%s:%s:%s\n",
Christian Gromm5a5abf02017-11-21 15:05:03 +0100477 c->pipe0.comp->name,
Christian Gromm723de0f2020-01-23 16:38:17 +0100478 dev_name(iface->dev),
Christian Gromm9136fcc2017-11-21 15:04:56 +0100479 dev_name(&c->dev));
480 }
Christian Gromm5a5abf02017-11-21 15:05:03 +0100481 if (c->pipe1.comp) {
Takashi Iwai234ff542020-03-11 10:19:44 +0100482 offs += scnprintf(buf + offs,
Christian Gromm9136fcc2017-11-21 15:04:56 +0100483 PAGE_SIZE - offs,
484 "%s:%s:%s\n",
Christian Gromm5a5abf02017-11-21 15:05:03 +0100485 c->pipe1.comp->name,
Christian Gromm723de0f2020-01-23 16:38:17 +0100486 dev_name(iface->dev),
Christian Gromm9136fcc2017-11-21 15:04:56 +0100487 dev_name(&c->dev));
Christian Grommbc5f96a2016-09-12 16:26:13 +0200488 }
489 }
Andrey Shvetsove7e3ce02017-11-21 15:05:22 +0100490 d->offs = offs;
Christian Gromm9136fcc2017-11-21 15:04:56 +0100491 return 0;
492}
493
Christian Grommd693e902020-01-23 16:38:20 +0100494static int most_match(struct device *dev, struct device_driver *drv)
495{
496 if (!strcmp(dev_name(dev), "most"))
497 return 0;
498 else
499 return 1;
500}
501
502static struct bus_type mostbus = {
503 .name = "most",
504 .match = most_match,
505};
506
Christian Gromm9136fcc2017-11-21 15:04:56 +0100507static ssize_t links_show(struct device_driver *drv, char *buf)
508{
Andrey Shvetsove7e3ce02017-11-21 15:05:22 +0100509 struct show_links_data d = { .buf = buf };
510
Christian Grommd693e902020-01-23 16:38:20 +0100511 bus_for_each_dev(&mostbus, NULL, &d, print_links);
Andrey Shvetsove7e3ce02017-11-21 15:05:22 +0100512 return d.offs;
Christian Gromm57562a72015-07-24 16:11:48 +0200513}
514
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100515static ssize_t components_show(struct device_driver *drv, char *buf)
Christian Grommbdafb7e2017-11-21 15:04:53 +0100516{
Christian Gromm45917e72019-12-13 13:04:15 +0100517 struct most_component *comp;
Christian Grommbdafb7e2017-11-21 15:04:53 +0100518 int offs = 0;
519
Christian Grommd693e902020-01-23 16:38:20 +0100520 list_for_each_entry(comp, &comp_list, list) {
Takashi Iwai234ff542020-03-11 10:19:44 +0100521 offs += scnprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
Christian Gromm5a5abf02017-11-21 15:05:03 +0100522 comp->name);
Christian Grommbdafb7e2017-11-21 15:04:53 +0100523 }
524 return offs;
525}
Quytelda Kahjaed49a3b2018-03-06 01:18:02 -0800526
Christian Gromm57562a72015-07-24 16:11:48 +0200527/**
Christian Grommb7937dc2017-11-21 15:05:12 +0100528 * get_channel - get pointer to channel
529 * @mdev: name of the device interface
530 * @mdev_ch: name of channel
Christian Gromm57562a72015-07-24 16:11:48 +0200531 */
Christian Grommec0c2f62017-11-21 15:04:55 +0100532static struct most_channel *get_channel(char *mdev, char *mdev_ch)
Christian Gromm57562a72015-07-24 16:11:48 +0200533{
Christian Gromm9136fcc2017-11-21 15:04:56 +0100534 struct device *dev = NULL;
535 struct most_interface *iface;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100536 struct most_channel *c, *tmp;
Christian Gromm57562a72015-07-24 16:11:48 +0200537
Christian Grommd693e902020-01-23 16:38:20 +0100538 dev = bus_find_device_by_name(&mostbus, NULL, mdev);
Christian Gromm9136fcc2017-11-21 15:04:56 +0100539 if (!dev)
540 return NULL;
Christian Gromm24850552020-01-23 16:38:18 +0100541 put_device(dev);
Christian Gromm723de0f2020-01-23 16:38:17 +0100542 iface = dev_get_drvdata(dev);
Christian Gromm9136fcc2017-11-21 15:04:56 +0100543 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
544 if (!strcmp(dev_name(&c->dev), mdev_ch))
545 return c;
Christian Gromm57562a72015-07-24 16:11:48 +0200546 }
Christian Gromm9136fcc2017-11-21 15:04:56 +0100547 return NULL;
Christian Gromm57562a72015-07-24 16:11:48 +0200548}
549
Christian Grommfcb7fad2017-11-21 15:04:47 +0100550static
Christian Grommdb09fe02017-11-21 15:05:04 +0100551inline int link_channel_to_component(struct most_channel *c,
Christian Gromm45917e72019-12-13 13:04:15 +0100552 struct most_component *comp,
Christian Grommdfee92d2019-04-03 15:19:45 +0200553 char *name,
Christian Grommdb09fe02017-11-21 15:05:04 +0100554 char *comp_param)
Christian Gromme6e79b42017-11-21 15:04:46 +0100555{
556 int ret;
Christian Gromm45917e72019-12-13 13:04:15 +0100557 struct most_component **comp_ptr;
Christian Gromme6e79b42017-11-21 15:04:46 +0100558
Christian Gromm5a5abf02017-11-21 15:05:03 +0100559 if (!c->pipe0.comp)
560 comp_ptr = &c->pipe0.comp;
561 else if (!c->pipe1.comp)
562 comp_ptr = &c->pipe1.comp;
Christian Gromme6e79b42017-11-21 15:04:46 +0100563 else
564 return -ENOSPC;
565
Christian Gromm5a5abf02017-11-21 15:05:03 +0100566 *comp_ptr = comp;
Christian Grommdfee92d2019-04-03 15:19:45 +0200567 ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, name,
568 comp_param);
Christian Gromme6e79b42017-11-21 15:04:46 +0100569 if (ret) {
Christian Gromm5a5abf02017-11-21 15:05:03 +0100570 *comp_ptr = NULL;
Christian Gromme6e79b42017-11-21 15:04:46 +0100571 return ret;
572 }
Christian Gromme6e79b42017-11-21 15:04:46 +0100573 return 0;
574}
575
Christian Gromm3d89b272019-04-03 15:19:46 +0200576int most_set_cfg_buffer_size(char *mdev, char *mdev_ch, u16 val)
577{
578 struct most_channel *c = get_channel(mdev, mdev_ch);
579
580 if (!c)
581 return -ENODEV;
582 c->cfg.buffer_size = val;
583 return 0;
584}
585
586int most_set_cfg_subbuffer_size(char *mdev, char *mdev_ch, u16 val)
587{
588 struct most_channel *c = get_channel(mdev, mdev_ch);
589
590 if (!c)
591 return -ENODEV;
592 c->cfg.subbuffer_size = val;
593 return 0;
594}
595
596int most_set_cfg_dbr_size(char *mdev, char *mdev_ch, u16 val)
597{
598 struct most_channel *c = get_channel(mdev, mdev_ch);
599
600 if (!c)
601 return -ENODEV;
602 c->cfg.dbr_size = val;
603 return 0;
604}
605
606int most_set_cfg_num_buffers(char *mdev, char *mdev_ch, u16 val)
607{
608 struct most_channel *c = get_channel(mdev, mdev_ch);
609
610 if (!c)
611 return -ENODEV;
612 c->cfg.num_buffers = val;
613 return 0;
614}
615
616int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
617{
618 int i;
619 struct most_channel *c = get_channel(mdev, mdev_ch);
620
621 if (!c)
622 return -ENODEV;
623 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
624 if (!strcmp(buf, ch_data_type[i].name)) {
625 c->cfg.data_type = ch_data_type[i].most_ch_data_type;
626 break;
627 }
628 }
629
630 if (i == ARRAY_SIZE(ch_data_type))
Christian Grommb7935e52020-01-23 16:38:21 +0100631 dev_warn(&c->dev, "Invalid attribute settings\n");
Christian Gromm3d89b272019-04-03 15:19:46 +0200632 return 0;
633}
634
635int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
636{
637 struct most_channel *c = get_channel(mdev, mdev_ch);
638
639 if (!c)
640 return -ENODEV;
Christian Gromm4df09912019-11-07 15:49:28 +0100641 if (!strcmp(buf, "dir_rx")) {
Christian Gromm3d89b272019-04-03 15:19:46 +0200642 c->cfg.direction = MOST_CH_RX;
Christian Gromm4df09912019-11-07 15:49:28 +0100643 } else if (!strcmp(buf, "rx")) {
Christian Gromm3d89b272019-04-03 15:19:46 +0200644 c->cfg.direction = MOST_CH_RX;
Christian Gromm4df09912019-11-07 15:49:28 +0100645 } else if (!strcmp(buf, "dir_tx")) {
Christian Gromm3d89b272019-04-03 15:19:46 +0200646 c->cfg.direction = MOST_CH_TX;
Christian Gromm4df09912019-11-07 15:49:28 +0100647 } else if (!strcmp(buf, "tx")) {
Christian Gromm3d89b272019-04-03 15:19:46 +0200648 c->cfg.direction = MOST_CH_TX;
649 } else {
Christian Gromm6a82c772020-01-23 16:38:19 +0100650 dev_err(&c->dev, "Invalid direction\n");
Christian Gromm3d89b272019-04-03 15:19:46 +0200651 return -ENODATA;
652 }
653 return 0;
654}
655
656int most_set_cfg_packets_xact(char *mdev, char *mdev_ch, u16 val)
657{
658 struct most_channel *c = get_channel(mdev, mdev_ch);
659
660 if (!c)
661 return -ENODEV;
662 c->cfg.packets_per_xact = val;
663 return 0;
664}
665
666int most_cfg_complete(char *comp_name)
667{
Christian Gromm45917e72019-12-13 13:04:15 +0100668 struct most_component *comp;
Christian Gromm3d89b272019-04-03 15:19:46 +0200669
670 comp = match_component(comp_name);
671 if (!comp)
672 return -ENODEV;
673
674 return comp->cfg_complete();
675}
676
677int most_add_link(char *mdev, char *mdev_ch, char *comp_name, char *link_name,
678 char *comp_param)
679{
Christian Grommacdbb892019-04-03 15:19:52 +0200680 struct most_channel *c = get_channel(mdev, mdev_ch);
Christian Gromm45917e72019-12-13 13:04:15 +0100681 struct most_component *comp = match_component(comp_name);
Christian Gromm3d89b272019-04-03 15:19:46 +0200682
Christian Grommacdbb892019-04-03 15:19:52 +0200683 if (!c || !comp)
Christian Gromm3d89b272019-04-03 15:19:46 +0200684 return -ENODEV;
685
686 return link_channel_to_component(c, comp, link_name, comp_param);
687}
Peikan Tsaif419f882019-08-26 01:58:49 +0800688
Christian Gromm3d89b272019-04-03 15:19:46 +0200689int most_remove_link(char *mdev, char *mdev_ch, char *comp_name)
690{
691 struct most_channel *c;
Christian Gromm45917e72019-12-13 13:04:15 +0100692 struct most_component *comp;
Christian Gromm3d89b272019-04-03 15:19:46 +0200693
694 comp = match_component(comp_name);
695 if (!comp)
696 return -ENODEV;
697 c = get_channel(mdev, mdev_ch);
698 if (!c)
699 return -ENODEV;
700
701 if (comp->disconnect_channel(c->iface, c->channel_id))
702 return -EIO;
703 if (c->pipe0.comp == comp)
704 c->pipe0.comp = NULL;
705 if (c->pipe1.comp == comp)
706 c->pipe1.comp = NULL;
707 return 0;
708}
709
Christian Grommbdafb7e2017-11-21 15:04:53 +0100710#define DRV_ATTR(_name) (&driver_attr_##_name.attr)
Christian Gromm57562a72015-07-24 16:11:48 +0200711
Christian Grommbdafb7e2017-11-21 15:04:53 +0100712static DRIVER_ATTR_RO(links);
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100713static DRIVER_ATTR_RO(components);
Christian Grommbdafb7e2017-11-21 15:04:53 +0100714
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100715static struct attribute *mc_attrs[] = {
Christian Grommbdafb7e2017-11-21 15:04:53 +0100716 DRV_ATTR(links),
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100717 DRV_ATTR(components),
Christian Gromm57562a72015-07-24 16:11:48 +0200718 NULL,
719};
720
Rikard Falkeborn26c2e922021-01-08 23:15:12 +0100721static const struct attribute_group mc_attr_group = {
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100722 .attrs = mc_attrs,
Christian Gromm57562a72015-07-24 16:11:48 +0200723};
724
Christian Grommfdbdc0e2017-11-21 15:05:13 +0100725static const struct attribute_group *mc_attr_groups[] = {
726 &mc_attr_group,
Christian Gromm4d5f0222017-11-21 15:04:43 +0100727 NULL,
728};
Christian Gromm57562a72015-07-24 16:11:48 +0200729
Christian Grommd693e902020-01-23 16:38:20 +0100730static struct device_driver mostbus_driver = {
731 .name = "most_core",
732 .bus = &mostbus,
733 .groups = mc_attr_groups,
734};
Christian Gromm921c80c2017-11-21 15:04:45 +0100735
Christian Gromm57562a72015-07-24 16:11:48 +0200736static inline void trash_mbo(struct mbo *mbo)
737{
738 unsigned long flags;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100739 struct most_channel *c = mbo->context;
Christian Gromm57562a72015-07-24 16:11:48 +0200740
741 spin_lock_irqsave(&c->fifo_lock, flags);
742 list_add(&mbo->list, &c->trash_fifo);
743 spin_unlock_irqrestore(&c->fifo_lock, flags);
744}
745
Christian Grommfcb7fad2017-11-21 15:04:47 +0100746static bool hdm_mbo_ready(struct most_channel *c)
Christian Gromm57562a72015-07-24 16:11:48 +0200747{
Christian Grommbf9503f2016-08-19 11:12:54 +0200748 bool empty;
Christian Gromm57562a72015-07-24 16:11:48 +0200749
Christian Grommbf9503f2016-08-19 11:12:54 +0200750 if (c->enqueue_halt)
751 return false;
752
753 spin_lock_irq(&c->fifo_lock);
754 empty = list_empty(&c->halt_fifo);
755 spin_unlock_irq(&c->fifo_lock);
756
757 return !empty;
Christian Gromm57562a72015-07-24 16:11:48 +0200758}
759
760static void nq_hdm_mbo(struct mbo *mbo)
761{
762 unsigned long flags;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100763 struct most_channel *c = mbo->context;
Christian Gromm57562a72015-07-24 16:11:48 +0200764
765 spin_lock_irqsave(&c->fifo_lock, flags);
766 list_add_tail(&mbo->list, &c->halt_fifo);
767 spin_unlock_irqrestore(&c->fifo_lock, flags);
768 wake_up_interruptible(&c->hdm_fifo_wq);
769}
770
771static int hdm_enqueue_thread(void *data)
772{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100773 struct most_channel *c = data;
Christian Gromm57562a72015-07-24 16:11:48 +0200774 struct mbo *mbo;
Christian Grommbf9503f2016-08-19 11:12:54 +0200775 int ret;
Christian Gromm57562a72015-07-24 16:11:48 +0200776 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
777
778 while (likely(!kthread_should_stop())) {
779 wait_event_interruptible(c->hdm_fifo_wq,
Christian Grommbf9503f2016-08-19 11:12:54 +0200780 hdm_mbo_ready(c) ||
Christian Gromm623d8002015-10-21 17:50:48 +0200781 kthread_should_stop());
Christian Gromm57562a72015-07-24 16:11:48 +0200782
Christian Grommbf9503f2016-08-19 11:12:54 +0200783 mutex_lock(&c->nq_mutex);
784 spin_lock_irq(&c->fifo_lock);
785 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
786 spin_unlock_irq(&c->fifo_lock);
787 mutex_unlock(&c->nq_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +0200788 continue;
Christian Grommbf9503f2016-08-19 11:12:54 +0200789 }
790
791 mbo = list_pop_mbo(&c->halt_fifo);
792 spin_unlock_irq(&c->fifo_lock);
Christian Gromm57562a72015-07-24 16:11:48 +0200793
794 if (c->cfg.direction == MOST_CH_RX)
795 mbo->buffer_length = c->cfg.buffer_size;
796
Christian Grommbf9503f2016-08-19 11:12:54 +0200797 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
798 mutex_unlock(&c->nq_mutex);
799
800 if (unlikely(ret)) {
Christian Grommb7935e52020-01-23 16:38:21 +0100801 dev_err(&c->dev, "Buffer enqueue failed\n");
Christian Gromm57562a72015-07-24 16:11:48 +0200802 nq_hdm_mbo(mbo);
803 c->hdm_enqueue_task = NULL;
804 return 0;
805 }
806 }
807
808 return 0;
809}
810
Christian Grommfcb7fad2017-11-21 15:04:47 +0100811static int run_enqueue_thread(struct most_channel *c, int channel_id)
Christian Gromm57562a72015-07-24 16:11:48 +0200812{
813 struct task_struct *task =
Shraddha Barke246ed512015-10-13 21:07:49 +0530814 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
815 channel_id);
Christian Gromm57562a72015-07-24 16:11:48 +0200816
817 if (IS_ERR(task))
818 return PTR_ERR(task);
819
820 c->hdm_enqueue_task = task;
821 return 0;
822}
823
824/**
825 * arm_mbo - recycle MBO for further usage
Christian Grommb7937dc2017-11-21 15:05:12 +0100826 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +0200827 *
828 * This puts an MBO back to the list to have it ready for up coming
829 * tx transactions.
830 *
831 * In case the MBO belongs to a channel that recently has been
832 * poisoned, the MBO is scheduled to be trashed.
Christian Grommb7937dc2017-11-21 15:05:12 +0100833 * Calls the completion handler of an attached component.
Christian Gromm57562a72015-07-24 16:11:48 +0200834 */
835static void arm_mbo(struct mbo *mbo)
836{
837 unsigned long flags;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100838 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +0200839
Christian Gromm57562a72015-07-24 16:11:48 +0200840 c = mbo->context;
841
842 if (c->is_poisoned) {
843 trash_mbo(mbo);
844 return;
845 }
846
847 spin_lock_irqsave(&c->fifo_lock, flags);
Christian Gromm71457d42015-09-28 17:18:45 +0200848 ++*mbo->num_buffers_ptr;
Christian Gromm57562a72015-07-24 16:11:48 +0200849 list_add_tail(&mbo->list, &c->fifo);
850 spin_unlock_irqrestore(&c->fifo_lock, flags);
851
Christian Gromm5a5abf02017-11-21 15:05:03 +0100852 if (c->pipe0.refs && c->pipe0.comp->tx_completion)
853 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
Christian Grommf13f6982015-09-28 17:18:35 +0200854
Christian Gromm5a5abf02017-11-21 15:05:03 +0100855 if (c->pipe1.refs && c->pipe1.comp->tx_completion)
856 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
Christian Gromm57562a72015-07-24 16:11:48 +0200857}
858
859/**
860 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
861 * @c: pointer to interface channel
862 * @dir: direction of the channel
863 * @compl: pointer to completion function
864 *
865 * This allocates buffer objects including the containing DMA coherent
866 * buffer and puts them in the fifo.
867 * Buffers of Rx channels are put in the kthread fifo, hence immediately
868 * submitted to the HDM.
869 *
870 * Returns the number of allocated and enqueued MBOs.
871 */
Christian Grommfcb7fad2017-11-21 15:04:47 +0100872static int arm_mbo_chain(struct most_channel *c, int dir,
Adrian Remondac942ea72015-08-14 12:18:00 +0200873 void (*compl)(struct mbo *))
Christian Gromm57562a72015-07-24 16:11:48 +0200874{
875 unsigned int i;
Christian Gromm57562a72015-07-24 16:11:48 +0200876 struct mbo *mbo;
Christian Grommaaf40322018-05-08 11:44:49 +0200877 unsigned long flags;
Christian Gromm2ae07512015-08-03 13:44:30 +0200878 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
Christian Gromm57562a72015-07-24 16:11:48 +0200879
880 atomic_set(&c->mbo_nq_level, 0);
881
882 for (i = 0; i < c->cfg.num_buffers; i++) {
883 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
Christian Grommaaf40322018-05-08 11:44:49 +0200884 if (!mbo)
885 goto flush_fifos;
886
Christian Gromm57562a72015-07-24 16:11:48 +0200887 mbo->context = c;
888 mbo->ifp = c->iface;
889 mbo->hdm_channel_id = c->channel_id;
Christian Gromm3598cec2018-05-08 11:45:03 +0200890 if (c->iface->dma_alloc) {
891 mbo->virt_address =
892 c->iface->dma_alloc(mbo, coherent_buf_size);
893 } else {
894 mbo->virt_address =
895 kzalloc(coherent_buf_size, GFP_KERNEL);
896 }
Christian Grommaaf40322018-05-08 11:44:49 +0200897 if (!mbo->virt_address)
898 goto release_mbo;
899
Christian Gromm57562a72015-07-24 16:11:48 +0200900 mbo->complete = compl;
Christian Gromm71457d42015-09-28 17:18:45 +0200901 mbo->num_buffers_ptr = &dummy_num_buffers;
Christian Gromm57562a72015-07-24 16:11:48 +0200902 if (dir == MOST_CH_RX) {
903 nq_hdm_mbo(mbo);
904 atomic_inc(&c->mbo_nq_level);
905 } else {
Christian Grommaaf40322018-05-08 11:44:49 +0200906 spin_lock_irqsave(&c->fifo_lock, flags);
907 list_add_tail(&mbo->list, &c->fifo);
908 spin_unlock_irqrestore(&c->fifo_lock, flags);
Christian Gromm57562a72015-07-24 16:11:48 +0200909 }
910 }
Christian Grommaaf40322018-05-08 11:44:49 +0200911 return c->cfg.num_buffers;
Christian Gromm57562a72015-07-24 16:11:48 +0200912
Christian Grommaaf40322018-05-08 11:44:49 +0200913release_mbo:
Christian Gromm57562a72015-07-24 16:11:48 +0200914 kfree(mbo);
Christian Grommaaf40322018-05-08 11:44:49 +0200915
916flush_fifos:
917 flush_channel_fifos(c);
918 return 0;
Christian Gromm57562a72015-07-24 16:11:48 +0200919}
920
921/**
922 * most_submit_mbo - submits an MBO to fifo
Christian Grommb7937dc2017-11-21 15:05:12 +0100923 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +0200924 */
Christian Gromma6f9d842016-09-21 14:49:05 +0200925void most_submit_mbo(struct mbo *mbo)
Christian Gromm57562a72015-07-24 16:11:48 +0200926{
Christian Gromma6f9d842016-09-21 14:49:05 +0200927 if (WARN_ONCE(!mbo || !mbo->context,
Christian Grommb7935e52020-01-23 16:38:21 +0100928 "Bad buffer or missing channel reference\n"))
Christian Gromma6f9d842016-09-21 14:49:05 +0200929 return;
Christian Gromm57562a72015-07-24 16:11:48 +0200930
931 nq_hdm_mbo(mbo);
Christian Gromm57562a72015-07-24 16:11:48 +0200932}
933EXPORT_SYMBOL_GPL(most_submit_mbo);
934
935/**
936 * most_write_completion - write completion handler
Christian Grommb7937dc2017-11-21 15:05:12 +0100937 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +0200938 *
939 * This recycles the MBO for further usage. In case the channel has been
940 * poisoned, the MBO is scheduled to be trashed.
941 */
942static void most_write_completion(struct mbo *mbo)
943{
Christian Grommfcb7fad2017-11-21 15:04:47 +0100944 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +0200945
Christian Gromm57562a72015-07-24 16:11:48 +0200946 c = mbo->context;
Sudip Mukherjeeec58d2a2015-09-04 16:22:06 +0530947 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
Christian Gromm57562a72015-07-24 16:11:48 +0200948 trash_mbo(mbo);
949 else
950 arm_mbo(mbo);
951}
952
Christian Gromm5a5abf02017-11-21 15:05:03 +0100953int channel_has_mbo(struct most_interface *iface, int id,
Christian Gromm45917e72019-12-13 13:04:15 +0100954 struct most_component *comp)
Christian Grommaac997d2015-09-28 17:18:57 +0200955{
Christian Gromm9136fcc2017-11-21 15:04:56 +0100956 struct most_channel *c = iface->p->channel[id];
Christian Grommaac997d2015-09-28 17:18:57 +0200957 unsigned long flags;
958 int empty;
959
960 if (unlikely(!c))
961 return -EINVAL;
962
Christian Grommf898f982017-11-21 15:04:50 +0100963 if (c->pipe0.refs && c->pipe1.refs &&
Christian Gromm5a5abf02017-11-21 15:05:03 +0100964 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
965 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
Christian Grommcdc293d2016-01-12 14:00:03 +0100966 return 0;
967
Christian Grommaac997d2015-09-28 17:18:57 +0200968 spin_lock_irqsave(&c->fifo_lock, flags);
969 empty = list_empty(&c->fifo);
970 spin_unlock_irqrestore(&c->fifo_lock, flags);
971 return !empty;
972}
973EXPORT_SYMBOL_GPL(channel_has_mbo);
974
Christian Gromm57562a72015-07-24 16:11:48 +0200975/**
976 * most_get_mbo - get pointer to an MBO of pool
977 * @iface: pointer to interface instance
978 * @id: channel ID
Christian Grommb7937dc2017-11-21 15:05:12 +0100979 * @comp: driver component
Christian Gromm57562a72015-07-24 16:11:48 +0200980 *
981 * This attempts to get a free buffer out of the channel fifo.
982 * Returns a pointer to MBO on success or NULL otherwise.
983 */
Christian Gromm71457d42015-09-28 17:18:45 +0200984struct mbo *most_get_mbo(struct most_interface *iface, int id,
Christian Gromm45917e72019-12-13 13:04:15 +0100985 struct most_component *comp)
Christian Gromm57562a72015-07-24 16:11:48 +0200986{
987 struct mbo *mbo;
Christian Grommfcb7fad2017-11-21 15:04:47 +0100988 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +0200989 unsigned long flags;
Christian Gromm71457d42015-09-28 17:18:45 +0200990 int *num_buffers_ptr;
Christian Gromm57562a72015-07-24 16:11:48 +0200991
Christian Gromm9136fcc2017-11-21 15:04:56 +0100992 c = iface->p->channel[id];
Christian Gromm57562a72015-07-24 16:11:48 +0200993 if (unlikely(!c))
994 return NULL;
Christian Gromm71457d42015-09-28 17:18:45 +0200995
Christian Grommf898f982017-11-21 15:04:50 +0100996 if (c->pipe0.refs && c->pipe1.refs &&
Christian Gromm5a5abf02017-11-21 15:05:03 +0100997 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
998 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
Christian Gromm71457d42015-09-28 17:18:45 +0200999 return NULL;
1000
Christian Gromm5a5abf02017-11-21 15:05:03 +01001001 if (comp == c->pipe0.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001002 num_buffers_ptr = &c->pipe0.num_buffers;
Christian Gromm5a5abf02017-11-21 15:05:03 +01001003 else if (comp == c->pipe1.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001004 num_buffers_ptr = &c->pipe1.num_buffers;
Christian Gromm71457d42015-09-28 17:18:45 +02001005 else
1006 num_buffers_ptr = &dummy_num_buffers;
1007
Christian Gromm57562a72015-07-24 16:11:48 +02001008 spin_lock_irqsave(&c->fifo_lock, flags);
1009 if (list_empty(&c->fifo)) {
1010 spin_unlock_irqrestore(&c->fifo_lock, flags);
1011 return NULL;
1012 }
1013 mbo = list_pop_mbo(&c->fifo);
Christian Gromm71457d42015-09-28 17:18:45 +02001014 --*num_buffers_ptr;
Christian Gromm57562a72015-07-24 16:11:48 +02001015 spin_unlock_irqrestore(&c->fifo_lock, flags);
Christian Gromm71457d42015-09-28 17:18:45 +02001016
1017 mbo->num_buffers_ptr = num_buffers_ptr;
Christian Gromm57562a72015-07-24 16:11:48 +02001018 mbo->buffer_length = c->cfg.buffer_size;
1019 return mbo;
1020}
1021EXPORT_SYMBOL_GPL(most_get_mbo);
1022
Christian Gromm57562a72015-07-24 16:11:48 +02001023/**
1024 * most_put_mbo - return buffer to pool
Christian Grommb7937dc2017-11-21 15:05:12 +01001025 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +02001026 */
1027void most_put_mbo(struct mbo *mbo)
1028{
Christian Grommfcb7fad2017-11-21 15:04:47 +01001029 struct most_channel *c = mbo->context;
Christian Gromm57562a72015-07-24 16:11:48 +02001030
Christian Gromm57562a72015-07-24 16:11:48 +02001031 if (c->cfg.direction == MOST_CH_TX) {
1032 arm_mbo(mbo);
1033 return;
1034 }
1035 nq_hdm_mbo(mbo);
1036 atomic_inc(&c->mbo_nq_level);
1037}
1038EXPORT_SYMBOL_GPL(most_put_mbo);
1039
1040/**
1041 * most_read_completion - read completion handler
Christian Grommb7937dc2017-11-21 15:05:12 +01001042 * @mbo: most buffer
Christian Gromm57562a72015-07-24 16:11:48 +02001043 *
1044 * This function is called by the HDM when data has been received from the
1045 * hardware and copied to the buffer of the MBO.
1046 *
1047 * In case the channel has been poisoned it puts the buffer in the trash queue.
Christian Grommb7937dc2017-11-21 15:05:12 +01001048 * Otherwise, it passes the buffer to an component for further processing.
Christian Gromm57562a72015-07-24 16:11:48 +02001049 */
1050static void most_read_completion(struct mbo *mbo)
1051{
Christian Grommfcb7fad2017-11-21 15:04:47 +01001052 struct most_channel *c = mbo->context;
Christian Gromm57562a72015-07-24 16:11:48 +02001053
Christian Grommf13f6982015-09-28 17:18:35 +02001054 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1055 trash_mbo(mbo);
1056 return;
1057 }
Christian Gromm57562a72015-07-24 16:11:48 +02001058
1059 if (mbo->status == MBO_E_INVAL) {
1060 nq_hdm_mbo(mbo);
1061 atomic_inc(&c->mbo_nq_level);
1062 return;
1063 }
1064
Christian Gromm5a63e232016-06-27 15:00:32 +02001065 if (atomic_sub_and_test(1, &c->mbo_nq_level))
Christian Gromm57562a72015-07-24 16:11:48 +02001066 c->is_starving = 1;
Christian Gromm57562a72015-07-24 16:11:48 +02001067
Christian Gromm5a5abf02017-11-21 15:05:03 +01001068 if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1069 c->pipe0.comp->rx_completion(mbo) == 0)
Christian Gromm57562a72015-07-24 16:11:48 +02001070 return;
Christian Grommf13f6982015-09-28 17:18:35 +02001071
Christian Gromm5a5abf02017-11-21 15:05:03 +01001072 if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1073 c->pipe1.comp->rx_completion(mbo) == 0)
Christian Gromm57562a72015-07-24 16:11:48 +02001074 return;
Christian Grommf13f6982015-09-28 17:18:35 +02001075
1076 most_put_mbo(mbo);
Christian Gromm57562a72015-07-24 16:11:48 +02001077}
1078
1079/**
1080 * most_start_channel - prepares a channel for communication
1081 * @iface: pointer to interface instance
1082 * @id: channel ID
Christian Grommb7937dc2017-11-21 15:05:12 +01001083 * @comp: driver component
Christian Gromm57562a72015-07-24 16:11:48 +02001084 *
1085 * This prepares the channel for usage. Cross-checks whether the
1086 * channel's been properly configured.
1087 *
1088 * Returns 0 on success or error code otherwise.
1089 */
Christian Grommf13f6982015-09-28 17:18:35 +02001090int most_start_channel(struct most_interface *iface, int id,
Christian Gromm45917e72019-12-13 13:04:15 +01001091 struct most_component *comp)
Christian Gromm57562a72015-07-24 16:11:48 +02001092{
1093 int num_buffer;
1094 int ret;
Christian Gromm9136fcc2017-11-21 15:04:56 +01001095 struct most_channel *c = iface->p->channel[id];
Christian Gromm57562a72015-07-24 16:11:48 +02001096
1097 if (unlikely(!c))
1098 return -EINVAL;
1099
Christian Grommf13f6982015-09-28 17:18:35 +02001100 mutex_lock(&c->start_mutex);
Christian Grommf898f982017-11-21 15:04:50 +01001101 if (c->pipe0.refs + c->pipe1.refs > 0)
Christian Grommb7937dc2017-11-21 15:05:12 +01001102 goto out; /* already started by another component */
Christian Gromm57562a72015-07-24 16:11:48 +02001103
1104 if (!try_module_get(iface->mod)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001105 dev_err(&c->dev, "Failed to acquire HDM lock\n");
Christian Grommf13f6982015-09-28 17:18:35 +02001106 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001107 return -ENOLCK;
1108 }
Christian Gromm57562a72015-07-24 16:11:48 +02001109
1110 c->cfg.extra_len = 0;
1111 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001112 dev_err(&c->dev, "Channel configuration failed. Go check settings...\n");
Christian Gromm57562a72015-07-24 16:11:48 +02001113 ret = -EINVAL;
Christian Grommbddd3c22018-09-21 11:28:51 +02001114 goto err_put_module;
Christian Gromm57562a72015-07-24 16:11:48 +02001115 }
1116
1117 init_waitqueue_head(&c->hdm_fifo_wq);
1118
1119 if (c->cfg.direction == MOST_CH_RX)
1120 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1121 most_read_completion);
1122 else
1123 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1124 most_write_completion);
Christian Gromm47af41b2015-10-15 13:28:57 +02001125 if (unlikely(!num_buffer)) {
Christian Gromm57562a72015-07-24 16:11:48 +02001126 ret = -ENOMEM;
Christian Grommbddd3c22018-09-21 11:28:51 +02001127 goto err_put_module;
Christian Gromm57562a72015-07-24 16:11:48 +02001128 }
1129
1130 ret = run_enqueue_thread(c, id);
1131 if (ret)
Christian Grommbddd3c22018-09-21 11:28:51 +02001132 goto err_put_module;
Christian Gromm57562a72015-07-24 16:11:48 +02001133
Christian Gromm57562a72015-07-24 16:11:48 +02001134 c->is_starving = 0;
Christian Grommf898f982017-11-21 15:04:50 +01001135 c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1136 c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
Christian Gromm57562a72015-07-24 16:11:48 +02001137 atomic_set(&c->mbo_ref, num_buffer);
Christian Grommf13f6982015-09-28 17:18:35 +02001138
1139out:
Christian Gromm5a5abf02017-11-21 15:05:03 +01001140 if (comp == c->pipe0.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001141 c->pipe0.refs++;
Christian Gromm5a5abf02017-11-21 15:05:03 +01001142 if (comp == c->pipe1.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001143 c->pipe1.refs++;
Christian Grommf13f6982015-09-28 17:18:35 +02001144 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001145 return 0;
Christian Grommf13f6982015-09-28 17:18:35 +02001146
Christian Grommbddd3c22018-09-21 11:28:51 +02001147err_put_module:
Markus Elfringe23afff2015-11-05 14:34:43 +01001148 module_put(iface->mod);
Christian Grommf13f6982015-09-28 17:18:35 +02001149 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001150 return ret;
1151}
1152EXPORT_SYMBOL_GPL(most_start_channel);
1153
1154/**
1155 * most_stop_channel - stops a running channel
1156 * @iface: pointer to interface instance
1157 * @id: channel ID
Christian Grommb7937dc2017-11-21 15:05:12 +01001158 * @comp: driver component
Christian Gromm57562a72015-07-24 16:11:48 +02001159 */
Christian Grommf13f6982015-09-28 17:18:35 +02001160int most_stop_channel(struct most_interface *iface, int id,
Christian Gromm45917e72019-12-13 13:04:15 +01001161 struct most_component *comp)
Christian Gromm57562a72015-07-24 16:11:48 +02001162{
Christian Grommfcb7fad2017-11-21 15:04:47 +01001163 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +02001164
1165 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
Christian Gromm6a82c772020-01-23 16:38:19 +01001166 pr_err("Bad interface or index out of range\n");
Christian Gromm57562a72015-07-24 16:11:48 +02001167 return -EINVAL;
1168 }
Christian Gromm9136fcc2017-11-21 15:04:56 +01001169 c = iface->p->channel[id];
Christian Gromm57562a72015-07-24 16:11:48 +02001170 if (unlikely(!c))
1171 return -EINVAL;
1172
Christian Grommf13f6982015-09-28 17:18:35 +02001173 mutex_lock(&c->start_mutex);
Christian Grommf898f982017-11-21 15:04:50 +01001174 if (c->pipe0.refs + c->pipe1.refs >= 2)
Christian Grommf13f6982015-09-28 17:18:35 +02001175 goto out;
Christian Gromm57562a72015-07-24 16:11:48 +02001176
Christian Gromm57562a72015-07-24 16:11:48 +02001177 if (c->hdm_enqueue_task)
1178 kthread_stop(c->hdm_enqueue_task);
1179 c->hdm_enqueue_task = NULL;
Christian Gromm57562a72015-07-24 16:11:48 +02001180
Christian Gromm9cda3002015-12-22 10:52:57 +01001181 if (iface->mod)
Christian Gromm57562a72015-07-24 16:11:48 +02001182 module_put(iface->mod);
Christian Gromm57562a72015-07-24 16:11:48 +02001183
1184 c->is_poisoned = true;
1185 if (c->iface->poison_channel(c->iface, c->channel_id)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001186 dev_err(&c->dev, "Failed to stop channel %d of interface %s\n", c->channel_id,
Christian Gromm78ce8b22020-01-14 16:57:51 +01001187 c->iface->description);
Christian Grommf13f6982015-09-28 17:18:35 +02001188 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001189 return -EAGAIN;
1190 }
1191 flush_trash_fifo(c);
1192 flush_channel_fifos(c);
1193
1194#ifdef CMPL_INTERRUPTIBLE
1195 if (wait_for_completion_interruptible(&c->cleanup)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001196 dev_err(&c->dev, "Interrupted while cleaning up channel %d\n", c->channel_id);
Christian Grommf13f6982015-09-28 17:18:35 +02001197 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001198 return -EINTR;
1199 }
1200#else
1201 wait_for_completion(&c->cleanup);
1202#endif
1203 c->is_poisoned = false;
Christian Grommf13f6982015-09-28 17:18:35 +02001204
1205out:
Christian Gromm5a5abf02017-11-21 15:05:03 +01001206 if (comp == c->pipe0.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001207 c->pipe0.refs--;
Christian Gromm5a5abf02017-11-21 15:05:03 +01001208 if (comp == c->pipe1.comp)
Christian Grommf898f982017-11-21 15:04:50 +01001209 c->pipe1.refs--;
Christian Grommf13f6982015-09-28 17:18:35 +02001210 mutex_unlock(&c->start_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001211 return 0;
1212}
1213EXPORT_SYMBOL_GPL(most_stop_channel);
1214
1215/**
Christian Grommb7937dc2017-11-21 15:05:12 +01001216 * most_register_component - registers a driver component with the core
1217 * @comp: driver component
Christian Gromm57562a72015-07-24 16:11:48 +02001218 */
Christian Gromm45917e72019-12-13 13:04:15 +01001219int most_register_component(struct most_component *comp)
Christian Gromm57562a72015-07-24 16:11:48 +02001220{
Christian Gromm5a5abf02017-11-21 15:05:03 +01001221 if (!comp) {
Christian Gromm6a82c772020-01-23 16:38:19 +01001222 pr_err("Bad component\n");
Christian Gromm57562a72015-07-24 16:11:48 +02001223 return -EINVAL;
1224 }
Christian Grommd693e902020-01-23 16:38:20 +01001225 list_add_tail(&comp->list, &comp_list);
Christian Gromm57562a72015-07-24 16:11:48 +02001226 return 0;
1227}
Christian Grommed021a02017-11-21 15:05:01 +01001228EXPORT_SYMBOL_GPL(most_register_component);
Christian Gromm57562a72015-07-24 16:11:48 +02001229
Christian Gromm9136fcc2017-11-21 15:04:56 +01001230static int disconnect_channels(struct device *dev, void *data)
1231{
1232 struct most_interface *iface;
1233 struct most_channel *c, *tmp;
Christian Gromm45917e72019-12-13 13:04:15 +01001234 struct most_component *comp = data;
Christian Gromm9136fcc2017-11-21 15:04:56 +01001235
Christian Gromm723de0f2020-01-23 16:38:17 +01001236 iface = dev_get_drvdata(dev);
Christian Gromm9136fcc2017-11-21 15:04:56 +01001237 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
Christian Gromm5a5abf02017-11-21 15:05:03 +01001238 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1239 comp->disconnect_channel(c->iface, c->channel_id);
1240 if (c->pipe0.comp == comp)
1241 c->pipe0.comp = NULL;
1242 if (c->pipe1.comp == comp)
1243 c->pipe1.comp = NULL;
Christian Gromm9136fcc2017-11-21 15:04:56 +01001244 }
1245 return 0;
1246}
1247
Christian Gromm57562a72015-07-24 16:11:48 +02001248/**
Christian Grommb7937dc2017-11-21 15:05:12 +01001249 * most_deregister_component - deregisters a driver component with the core
1250 * @comp: driver component
Christian Gromm57562a72015-07-24 16:11:48 +02001251 */
Christian Gromm45917e72019-12-13 13:04:15 +01001252int most_deregister_component(struct most_component *comp)
Christian Gromm57562a72015-07-24 16:11:48 +02001253{
Christian Gromm5a5abf02017-11-21 15:05:03 +01001254 if (!comp) {
Christian Gromm6a82c772020-01-23 16:38:19 +01001255 pr_err("Bad component\n");
Christian Gromm57562a72015-07-24 16:11:48 +02001256 return -EINVAL;
1257 }
1258
Christian Grommd693e902020-01-23 16:38:20 +01001259 bus_for_each_dev(&mostbus, NULL, comp, disconnect_channels);
Christian Gromm5a5abf02017-11-21 15:05:03 +01001260 list_del(&comp->list);
Christian Gromm57562a72015-07-24 16:11:48 +02001261 return 0;
1262}
Christian Grommed021a02017-11-21 15:05:01 +01001263EXPORT_SYMBOL_GPL(most_deregister_component);
Christian Gromm57562a72015-07-24 16:11:48 +02001264
Christian Gromm4d5f0222017-11-21 15:04:43 +01001265static void release_channel(struct device *dev)
1266{
Christian Gromm723de0f2020-01-23 16:38:17 +01001267 struct most_channel *c = to_channel(dev);
1268
1269 kfree(c);
Christian Gromm4d5f0222017-11-21 15:04:43 +01001270}
1271
Christian Gromm57562a72015-07-24 16:11:48 +02001272/**
1273 * most_register_interface - registers an interface with core
Christian Grommb7937dc2017-11-21 15:05:12 +01001274 * @iface: device interface
Christian Gromm57562a72015-07-24 16:11:48 +02001275 *
1276 * Allocates and initializes a new interface instance and all of its channels.
1277 * Returns a pointer to kobject or an error pointer.
1278 */
Christian Gromm4d5f0222017-11-21 15:04:43 +01001279int most_register_interface(struct most_interface *iface)
Christian Gromm57562a72015-07-24 16:11:48 +02001280{
1281 unsigned int i;
1282 int id;
Christian Grommfcb7fad2017-11-21 15:04:47 +01001283 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +02001284
1285 if (!iface || !iface->enqueue || !iface->configure ||
Colin Ian Kinge4463e42020-06-24 17:39:57 +01001286 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS))
Christian Gromm4d5f0222017-11-21 15:04:43 +01001287 return -EINVAL;
Christian Gromm57562a72015-07-24 16:11:48 +02001288
1289 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1290 if (id < 0) {
Christian Grommb7935e52020-01-23 16:38:21 +01001291 dev_err(iface->dev, "Failed to allocate device ID\n");
Christian Gromm4d5f0222017-11-21 15:04:43 +01001292 return id;
Christian Gromm57562a72015-07-24 16:11:48 +02001293 }
Christian Gromm57562a72015-07-24 16:11:48 +02001294
Christian Gromm9136fcc2017-11-21 15:04:56 +01001295 iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1296 if (!iface->p) {
Christian Grommb7382d42015-12-22 10:52:51 +01001297 ida_simple_remove(&mdev_id, id);
Christian Gromm4d5f0222017-11-21 15:04:43 +01001298 return -ENOMEM;
Christian Gromm57562a72015-07-24 16:11:48 +02001299 }
1300
Christian Gromm9136fcc2017-11-21 15:04:56 +01001301 INIT_LIST_HEAD(&iface->p->channel_list);
1302 iface->p->dev_id = id;
Gustavo A. R. Silva3970d0d2019-04-22 10:40:38 -05001303 strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
Christian Grommd693e902020-01-23 16:38:20 +01001304 iface->dev->bus = &mostbus;
Christian Gromm723de0f2020-01-23 16:38:17 +01001305 iface->dev->groups = interface_attr_groups;
1306 dev_set_drvdata(iface->dev, iface);
1307 if (device_register(iface->dev)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001308 dev_err(iface->dev, "Failed to register interface device\n");
Christian Gromm9136fcc2017-11-21 15:04:56 +01001309 kfree(iface->p);
Christian Gromm723de0f2020-01-23 16:38:17 +01001310 put_device(iface->dev);
Christian Gromm4d5f0222017-11-21 15:04:43 +01001311 ida_simple_remove(&mdev_id, id);
1312 return -ENOMEM;
1313 }
Christian Gromm57562a72015-07-24 16:11:48 +02001314
1315 for (i = 0; i < iface->num_channels; i++) {
1316 const char *name_suffix = iface->channel_vector[i].name_suffix;
1317
Christian Gromm4d5f0222017-11-21 15:04:43 +01001318 c = kzalloc(sizeof(*c), GFP_KERNEL);
Christian Gromm57562a72015-07-24 16:11:48 +02001319 if (!c)
Christian Grommbddd3c22018-09-21 11:28:51 +02001320 goto err_free_resources;
Christian Gromm845101b2017-11-21 15:04:57 +01001321 if (!name_suffix)
1322 snprintf(c->name, STRING_SIZE, "ch%d", i);
1323 else
1324 snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1325 c->dev.init_name = c->name;
Christian Gromm723de0f2020-01-23 16:38:17 +01001326 c->dev.parent = iface->dev;
Christian Gromm4d5f0222017-11-21 15:04:43 +01001327 c->dev.groups = channel_attr_groups;
1328 c->dev.release = release_channel;
Christian Gromm9136fcc2017-11-21 15:04:56 +01001329 iface->p->channel[i] = c;
Christian Gromm57562a72015-07-24 16:11:48 +02001330 c->is_starving = 0;
1331 c->iface = iface;
Christian Gromm57562a72015-07-24 16:11:48 +02001332 c->channel_id = i;
1333 c->keep_mbo = false;
1334 c->enqueue_halt = false;
1335 c->is_poisoned = false;
Christian Gromm57562a72015-07-24 16:11:48 +02001336 c->cfg.direction = 0;
1337 c->cfg.data_type = 0;
1338 c->cfg.num_buffers = 0;
1339 c->cfg.buffer_size = 0;
1340 c->cfg.subbuffer_size = 0;
1341 c->cfg.packets_per_xact = 0;
1342 spin_lock_init(&c->fifo_lock);
1343 INIT_LIST_HEAD(&c->fifo);
1344 INIT_LIST_HEAD(&c->trash_fifo);
1345 INIT_LIST_HEAD(&c->halt_fifo);
1346 init_completion(&c->cleanup);
1347 atomic_set(&c->mbo_ref, 0);
Christian Grommf13f6982015-09-28 17:18:35 +02001348 mutex_init(&c->start_mutex);
Christian Grommbf9503f2016-08-19 11:12:54 +02001349 mutex_init(&c->nq_mutex);
Christian Gromm9136fcc2017-11-21 15:04:56 +01001350 list_add_tail(&c->list, &iface->p->channel_list);
Christian Grommf0b4a222018-08-13 15:02:28 +02001351 if (device_register(&c->dev)) {
Christian Grommb7935e52020-01-23 16:38:21 +01001352 dev_err(&c->dev, "Failed to register channel device\n");
Christian Grommbddd3c22018-09-21 11:28:51 +02001353 goto err_free_most_channel;
Christian Grommf0b4a222018-08-13 15:02:28 +02001354 }
Christian Gromm57562a72015-07-24 16:11:48 +02001355 }
Christian Grommacdbb892019-04-03 15:19:52 +02001356 most_interface_register_notify(iface->description);
Christian Gromm4d5f0222017-11-21 15:04:43 +01001357 return 0;
Christian Gromm57562a72015-07-24 16:11:48 +02001358
Christian Grommbddd3c22018-09-21 11:28:51 +02001359err_free_most_channel:
Christian Gromm723de0f2020-01-23 16:38:17 +01001360 put_device(&c->dev);
Christian Gromm9136fcc2017-11-21 15:04:56 +01001361
Christian Grommbddd3c22018-09-21 11:28:51 +02001362err_free_resources:
Christian Gromm9136fcc2017-11-21 15:04:56 +01001363 while (i > 0) {
1364 c = iface->p->channel[--i];
1365 device_unregister(&c->dev);
Christian Gromm9136fcc2017-11-21 15:04:56 +01001366 }
1367 kfree(iface->p);
Christian Gromm723de0f2020-01-23 16:38:17 +01001368 device_unregister(iface->dev);
Christian Grommb7382d42015-12-22 10:52:51 +01001369 ida_simple_remove(&mdev_id, id);
Christian Gromm4d5f0222017-11-21 15:04:43 +01001370 return -ENOMEM;
Christian Gromm57562a72015-07-24 16:11:48 +02001371}
1372EXPORT_SYMBOL_GPL(most_register_interface);
1373
1374/**
1375 * most_deregister_interface - deregisters an interface with core
Christian Grommb7937dc2017-11-21 15:05:12 +01001376 * @iface: device interface
Christian Gromm57562a72015-07-24 16:11:48 +02001377 *
1378 * Before removing an interface instance from the list, all running
1379 * channels are stopped and poisoned.
1380 */
1381void most_deregister_interface(struct most_interface *iface)
1382{
Christian Gromm4d5f0222017-11-21 15:04:43 +01001383 int i;
Christian Grommfcb7fad2017-11-21 15:04:47 +01001384 struct most_channel *c;
Christian Gromm57562a72015-07-24 16:11:48 +02001385
Christian Gromm4d5f0222017-11-21 15:04:43 +01001386 for (i = 0; i < iface->num_channels; i++) {
Christian Gromm9136fcc2017-11-21 15:04:56 +01001387 c = iface->p->channel[i];
Christian Gromm5a5abf02017-11-21 15:05:03 +01001388 if (c->pipe0.comp)
1389 c->pipe0.comp->disconnect_channel(c->iface,
Christian Gromma0fceb12015-12-22 10:52:55 +01001390 c->channel_id);
Christian Gromm5a5abf02017-11-21 15:05:03 +01001391 if (c->pipe1.comp)
1392 c->pipe1.comp->disconnect_channel(c->iface,
Christian Gromma0fceb12015-12-22 10:52:55 +01001393 c->channel_id);
Christian Gromm5a5abf02017-11-21 15:05:03 +01001394 c->pipe0.comp = NULL;
1395 c->pipe1.comp = NULL;
Christian Gromm4d5f0222017-11-21 15:04:43 +01001396 list_del(&c->list);
1397 device_unregister(&c->dev);
Christian Gromma0fceb12015-12-22 10:52:55 +01001398 }
1399
Christian Gromm9136fcc2017-11-21 15:04:56 +01001400 ida_simple_remove(&mdev_id, iface->p->dev_id);
1401 kfree(iface->p);
Christian Gromm723de0f2020-01-23 16:38:17 +01001402 device_unregister(iface->dev);
Christian Gromm57562a72015-07-24 16:11:48 +02001403}
1404EXPORT_SYMBOL_GPL(most_deregister_interface);
1405
1406/**
1407 * most_stop_enqueue - prevents core from enqueueing MBOs
1408 * @iface: pointer to interface
1409 * @id: channel id
1410 *
1411 * This is called by an HDM that _cannot_ attend to its duties and
1412 * is imminent to get run over by the core. The core is not going to
1413 * enqueue any further packets unless the flagging HDM calls
1414 * most_resume enqueue().
1415 */
1416void most_stop_enqueue(struct most_interface *iface, int id)
1417{
Christian Gromm9136fcc2017-11-21 15:04:56 +01001418 struct most_channel *c = iface->p->channel[id];
Christian Gromm57562a72015-07-24 16:11:48 +02001419
Christian Grommbf9503f2016-08-19 11:12:54 +02001420 if (!c)
1421 return;
1422
1423 mutex_lock(&c->nq_mutex);
1424 c->enqueue_halt = true;
1425 mutex_unlock(&c->nq_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001426}
1427EXPORT_SYMBOL_GPL(most_stop_enqueue);
1428
1429/**
1430 * most_resume_enqueue - allow core to enqueue MBOs again
1431 * @iface: pointer to interface
1432 * @id: channel id
1433 *
1434 * This clears the enqueue halt flag and enqueues all MBOs currently
1435 * sitting in the wait fifo.
1436 */
1437void most_resume_enqueue(struct most_interface *iface, int id)
1438{
Christian Gromm9136fcc2017-11-21 15:04:56 +01001439 struct most_channel *c = iface->p->channel[id];
Christian Gromm57562a72015-07-24 16:11:48 +02001440
Christian Grommbf9503f2016-08-19 11:12:54 +02001441 if (!c)
Christian Gromm57562a72015-07-24 16:11:48 +02001442 return;
Christian Grommbf9503f2016-08-19 11:12:54 +02001443
1444 mutex_lock(&c->nq_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001445 c->enqueue_halt = false;
Christian Grommbf9503f2016-08-19 11:12:54 +02001446 mutex_unlock(&c->nq_mutex);
Christian Gromm57562a72015-07-24 16:11:48 +02001447
1448 wake_up_interruptible(&c->hdm_fifo_wq);
1449}
1450EXPORT_SYMBOL_GPL(most_resume_enqueue);
1451
1452static int __init most_init(void)
1453{
Sudip Mukherjeecc4188b2016-02-08 23:26:46 +05301454 int err;
1455
Christian Grommd693e902020-01-23 16:38:20 +01001456 INIT_LIST_HEAD(&comp_list);
Christian Gromm57562a72015-07-24 16:11:48 +02001457 ida_init(&mdev_id);
1458
Christian Grommd693e902020-01-23 16:38:20 +01001459 err = bus_register(&mostbus);
Sudip Mukherjeecc4188b2016-02-08 23:26:46 +05301460 if (err) {
Christian Grommb7935e52020-01-23 16:38:21 +01001461 pr_err("Failed to register most bus\n");
Sudip Mukherjeecc4188b2016-02-08 23:26:46 +05301462 return err;
Christian Gromm57562a72015-07-24 16:11:48 +02001463 }
Christian Grommd693e902020-01-23 16:38:20 +01001464 err = driver_register(&mostbus_driver);
Sudip Mukherjeecc4188b2016-02-08 23:26:46 +05301465 if (err) {
Christian Grommb7935e52020-01-23 16:38:21 +01001466 pr_err("Failed to register core driver\n");
Christian Grommbddd3c22018-09-21 11:28:51 +02001467 goto err_unregister_bus;
Christian Gromm57562a72015-07-24 16:11:48 +02001468 }
Christian Gromm919c03a2019-04-03 15:19:48 +02001469 configfs_init();
Christian Gromm57562a72015-07-24 16:11:48 +02001470 return 0;
1471
Christian Grommbddd3c22018-09-21 11:28:51 +02001472err_unregister_bus:
Christian Grommd693e902020-01-23 16:38:20 +01001473 bus_unregister(&mostbus);
Sudip Mukherjeecc4188b2016-02-08 23:26:46 +05301474 return err;
Christian Gromm57562a72015-07-24 16:11:48 +02001475}
1476
1477static void __exit most_exit(void)
1478{
Christian Grommd693e902020-01-23 16:38:20 +01001479 driver_unregister(&mostbus_driver);
1480 bus_unregister(&mostbus);
Christian Gromm57562a72015-07-24 16:11:48 +02001481 ida_destroy(&mdev_id);
1482}
1483
Christian Gromm5e56bc02020-04-24 17:16:34 +02001484subsys_initcall(most_init);
Christian Gromm57562a72015-07-24 16:11:48 +02001485module_exit(most_exit);
1486MODULE_LICENSE("GPL");
1487MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1488MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");